Question:
PHP MYSQL Database HELP!!?
2009-12-07 23:00:45 UTC
Hi! I'm trying to get my PHP webpage to pull data from my database into a table. I keep getting:

"mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource"


switch (show_files){ // Switch statement to display only certain records//
case ($_GET["next"] == NULL): // Displays items 1-10//
$connect = mysql_connect("localhost", "mayonaka","182007","grocery"); // connect to database//
if (!$connect){
echo "

The database server is not available!";} //If the database cannot connect display message//
$rownum=1;
echo '

';
$txtquery= "SELECT vchrNAME, fltPRICE FROM catalog WHERE intID = '1'";
$result= mysql_query($txtquery);
while ($row=mysql_fetch_assoc($result)) {
echo '';
}
echo '
'.$row[0].''.$row[1].''.$row[2].'
';


Thanks!
Four answers:
just "JR"
2009-12-08 00:41:02 UTC
Josh is nearly correct.

Connecting to db:

NEVER pass your usernames, passwords, dbnames on this site or on any other. Since you have done it, after this, change them!

Define your names first:

define ("DBHOST", "localhost");

define ("DBUSER", "mayonaka");

define ("DBPWD", "182007");

define ("DBNAME", "grocery");

then make a dbconnect call:

function dbconnect()

{

$link = mysql_connect(DBHOST, DBUSER, DBPWD) or die ("Error: ".mysql_error());

mysql_select_db(DBNAME) or die("Could not select database: ".mysql_error());

return ($link);

}

-------

store these in a different file (hidden)

====

Your switch has no meaning: show_files is NOT a variable!

I assume you are trying to display rows 1-10, 11-20 etc

function show($id, $first) // receives two parameters: the ID (for the search), and the first record to show)

{

$link = dbconnect();

$sql = "select `vchrNAME`,`fltPRICE` from `catalog` where `intID` = '".$id."' limit $first, 10";

// LIMIT to display 10 rows from "first" (1-10, 11,20 etc)

$res = mysql_query($sql) or die (mysql_error());

// your current error is that your query is incorrect: you have not selected a database. Since you have no check on your query, when you get to the function mysql_fetch_assoc, the ressource provided ($result in your code) does not exist, hence, error

echo (""); // you did not start your table!

while ($row = mysql_fetch_array($res))

// fetch_assoc will return array['NAME']

// fetch_array will return array['NAME'] and accepts array[0]

{

echo ( "";

}

echo ("
" . $row['vchrNAME'] . "" . $row['fltPRICE'] . "
"); // you did not close your table

mysql_free_result ($res); // always free your ressource: you build up in memory!

mysql_close($link); // CLOSE your database..

}



Now, to get the entries, just set "$first" and call the function.

$id = something;

show ($id, "1");

show ($id, "11");

show ($id, "21"), etc...
josh.rofl
2009-12-07 23:23:10 UTC
First, for future programming questions, ask them at http://stackoverflow.com. Yahoo Answers is horrible at displaying code.



Second, you have to select a database with mysql_select_db() after connecting.



Third, you will have to put a link_identifier into your mysql_query statement.





/*

* Switch statement to display only certain records

*/

switch (show_files) {

case ($_GET["next"] == NULL): // Displays items 1-10 //



/*

* Connect to MySQL database.

*/

$connect = mysql_connect("localhost", "mayonaka", "182007");



/*

* Show error: database cannot connect.

*/

if (!$connect) {

echo "

The database server is not available!

";

} else {



/*

* Select MySQL database.

*/

mysql_select_db("grocery", $connect);



$rownum = 1;

echo '';



$query = "SELECT `vchrNAME`, `fltPRICE` FROM `catalog` WHERE `intID` = '1'";

$result = mysql_query($query, $connect);



while ($row = mysql_fetch_assoc($result))

{

echo '\n';

}



echo '
'.$row[0].''.$row[1].'
';



}
2016-10-29 08:36:44 UTC
a million) Use an account that has a password. I doubt MySQL will enable a connection without a password from Hypertext Preprocessor. try putting a password for root and sorting out with this. 2) make beneficial MySQL provider is working. 3) be sure you're figuring out the host on which the database is saved wisely once you place up in Dreamweaver ie "localhost" or "127.0.0.a million". 4) make beneficial that the person bills you're trying have permissions to get right of entry to the particular MySQL database. 5) If those steps fail then attempt to connect manually by using pasting here right into a .Hypertext Preprocessor web site and beginning it in a browser. substitute person, password and yourdbname to remarkable values. in case you don't get a 'errors connecting to mysql' errors, then your connection is working superb. in case you extremely can no longer get it working in Dreamweaver, then I advise you basically examine some internet tutorials and connect with the database manually. that's plenty greater straightforward interior the long-term.
askernanswerer
2009-12-07 23:23:03 UTC
in case of mysql_fetch_row use array subscript number but in case of mysql_fetch_assoc use the column name as array subscript. So try this:

echo "".$row['vchrNAME']."".$row['fltPRICE'].....


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...