0

I was trying to connect to my database through PHP but I keep getting the error:

Warning: mysql_fetch_array() expects parameter 1 to be resource

I do not know what the problem is?

This is the code I have:

<?php
  //Connect to the server
  $connect = mysql_connect("localhost","root","");

  //Connect to the database
  mysql_select_db("firstdatab");

  $query = mysql_query("SELECT * FROM users WHERE FavNumber = '44' ");
  //Get Results

  $rows = mysql_fetch_array($query);
  $first_name = $rows['Name'];
  echo "$first_name";
?>

Can someone tell me what is wrong?

  • I don't know why this won't migrate to StackOverflow. – John Conde Jun 14 '12 at 15:10
  • @JohnConde I am blocked on Stack Overflow. I dont know why and I cannot fix it. – Coder404 Jun 14 '12 at 15:19
  • Asking coding questions on Webmasters is not the appropriate course of action. – danlefree Jun 14 '12 at 16:19
1

Everything in your code looks fine so your problem lies elsewhere. You need to use mysql_error() to tell you what you problem is.

<?php


//Connect to the server
$connect = mysql_connect("localhost","root","");

//Connect to the database
mysql_select_db("firstdatab");


$results = mysql_query("SELECT * FROM users WHERE FavNumber = '44' ");

if ($results)
{
    $rows = mysql_fetch_array($results);
    $first_name = $rows['Name'];
    echo "$first_name";
}
else
{
    echo mysql_error();
}

?>
| improve this answer | |
  • I just tried it and it came back with: Table 'firstdatab.users' doesn't exist – Coder404 Jun 14 '12 at 15:08
  • 2
    There's your problem right there. – John Conde Jun 14 '12 at 15:09
  • what does that mean? – Coder404 Jun 14 '12 at 15:11
  • Sorry Im kind of new to php and mysql – Coder404 Jun 14 '12 at 15:11
  • Execute these: 'CREATE DATABASE IF NOT EXISTS firstdatab;' then 'CREATE TABLE firstdatab.users (Name VARCHAR(32) NOT NULL) ENGINE = MyISAM;' and insert your data. – ionFish Jun 14 '12 at 15:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.