PDA

View Full Version : php/mysql.... HELP



i3astard
23rd January 2006, 12:20 AM
This is my code...

<html>

<body>

<?php

$dbh=mysql_connect ("localhost", "willswan_wehco", "snot") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("willswan_wehco");


$result = mysql_query("SELECT * FROM willswan_wehco.Title",$db);
printf("System: %s<br>\n", mysql_result($result,0,"co title"));
printf("Last Name: %s<br>\n", mysql_result($result,0,"last"));
printf("Address: %s<br>\n", mysql_result($result,0,"address"));
printf("Position: %s<br>\n", mysql_result($result,0,"position"));
?>

</body>

</html>

and this is the error i'm getting...

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/willswan/public_html/snot.php on line 13

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/willswan/public_html/snot.php on line 17
System:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/willswan/public_html/snot.php on line 21
Last Name:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/willswan/public_html/snot.php on line 25
Address:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/willswan/public_html/snot.php on line 29
Position:

if anyone can tell me why it's not printing the results...I'd greatly appreciate it. I believe it's connecting to the database, because if it wasn't wouldn't it print that out??

Xull
23rd January 2006, 02:53 AM
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/willswan/public_html/snot.php on line 13that would be whats causing the other errors... If it can't make a valid query, it can't exactly get the info to display it...


$result = mysql_query("SELECT * FROM willswan_wehco.Title",$db);

resource mysql_query ( string query [, resource link_identifier] )the link resource you are passing is $db, but you have not set that anywhere, so it is not valid...

You'd set it here, but here you are using the variable name $dbh instead of $db:

$dbh=mysql_connect ("localhost", "willswan_wehco", "snot") or die ('I cannot connect to the database because: ' . mysql_error());

so... seems like a simple typo/wrong variable name... try changing both to the same name.

Killer-Rival
6th February 2006, 04:54 AM
eww. you need functions.

heres the code for my php / mysql portal (open source).
http://www.rival.ionws.com


<?php
/************************************************** ***********\
Rival Webs Version 0.5
************************************************** ***********
MYSQL.LIB.PHP
Has all the MySQL query functions and connection functions
here. DO NOT REMOVE THIS FILE, the entire site will fall
apart.

All functions return a value of true/false. There are no
echo's here.
\************************************************* ************/
error_reporting(E_ALL);
/* Query Functions
sql_connect() - Connects to mysql databse.
sql_select() - Queries one row of a table.
sql_select_all() - Queries an entire MySQL table.
sql_insert() - Creates a row.
sql_update() - Updates a row.
sql_delete() - Deletes a row.
*/

function sql_connect($server= null, $username= null, $password= null, $database= null){
$connect = mysql_connect($server, $username, $password);
$db = mysql_select_db($database);
if(!$connect){
return false;
// MySQL could not connect. Run the error variables!
} else {
return true;
// MySQL is connected. Run the rest of the site.
}
}

/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */

function sql_select($table = null, $condition = null, $field = null){
if(is_null($table) || is_null($condition)){
// Something wasn't set, cannot process the query.
return false;
}

$i = 0;
$condition_string = '';
$last = count($condition) - 1;
foreach ($condition as $a => $b) {
$condition_string .= "`".$a."` = '".$b."'";
if ($i < $last)
$condition_string .= "AND ";
$i++;
}

if(is_null($field)){
$field_string = "*";
} else {
$field_string = "`".$field."`";
}

$query = "SELECT ".$field_string." from `".$table."` WHERE ".$condition_string;
$sql = mysql_query($query);
if(!$sql){
// Nothing returned.
return false;
} else {
return mysql_fetch_array($sql);
}
}

/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */

function sql_select_all($table= null, $field= null, $sort= null){
if(is_null($table)){
return false;
}
if(is_null($sort) || is_null($field)){
$query = "SELECT * from `".$table."`";
} else {
$query = "SELECT * from `".$table."` ORDER BY `".$field."` ".$sort;
}

$sql = mysql_query($query);

while($row = mysql_fetch_array($sql)){
$return[] = $row;
}

if(empty($return))
return false;
else
return $return;
}

/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */

function sql_insert($table= null, $values= null){
if(is_null($table) || is_null($values)){
return false;
}

$i = 0;
$values_string = '';
$last = count($values) - 1;
foreach ($values as $item) {
$values_string .= "'".$item."'";
if ($i < $last)
$values_string .= ', ';
$i++;
}

$query = "INSERT INTO ".$table." VALUES(".$values_string.")";
$sql = mysql_query($query);

if(!$query){
return false;
} else {
return true;
}
}

/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */

function sql_update($table= null, $fields= null, $condition= null){
if(is_null($table) || is_null($fields) || is_null($condition)){
return false;
}

$i = 0;
$field_string = '';
$last = count($fields) - 1;
foreach ($fields as $a => $b) {
$field_string .= "`".$a."`='".$b."'";
if ($i < $last)
$field_string .= ', ';
$i++;
}

$i = 0;
$condition_string = '';
$last = count($condition) - 1;
foreach ($condition as $a => $b) {
$condition_string .= "`".$a."`='".$b."'";
if ($i < $last){
$condition_string .= 'AND ';
}
$i++;
}

$query = "UPDATE `".$table."` SET ".$field_string." WHERE ".$condition_string." LIMIT 1";
$sql = mysql_query($query);

if(!$sql)
return false;
else
return true;
}

/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */
/* :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::: */

function sql_delete($table= null, $condition= null){
if(is_null($table) || is_null($condition)){
return false;
}

$i = 0;
$condition_string = '';
$last = count($condition) - 1;
foreach ($condition as $a => $b) {
$condition_string .= "`".$a."`='".$b."'";
if ($i < $last)
$condition_string .= 'AND ';
$i++;
}

$query = 'DELETE FROM `'.$table.'` WHERE '.$condition_string;
$sql = mysql_query($query);
if (!$sql) {
return false;
} else
return true;
}
?>

PsychoticDude85
6th February 2006, 06:10 AM
mysql_select_db ("willswan_wehco"); is technically correct, but according to php.net it should be

mysql_select_db ("willswan_wehco",$dbh);
http://uk2.php.net/mysql_select_db

But functions would clean that up a lot it's true. I would say it is most likely that you need to 'h' on the end of $db as was suggested.

Carnage
6th February 2006, 04:21 PM
eww. you need functions.

heres the code for my php / mysql portal (open source).
http://www.rival.ionws.com


see, i think that all those functions are a bit over the top... ALL you need a php/mysql interface to do is catch errors (oh and also to add in the nice function query_first (Selects the first row given by a query and returns it as an array, since alot of the queries you will want to do will only return one row this function saves a lot of programming time))

Writing something to simplify writing queries is pointless since it wastes processing time

Killer-Rival
6th February 2006, 07:30 PM
meh, i like em. they work awesome for my portal.