Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

closed as off topic by John Conde Jun 15 '12 at 11:32

Questions on Webmasters Stack Exchange are expected to relate to webmastering within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 1 down vote accepted

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();
}

?>
share|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
add comment

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