Newbie Help with php connection to mysql 
Author Message
 Newbie Help with php connection to mysql

I'm attempting to use a basic script to connect to a mysql database
and just print the first record to ensure the connections and
everything are working. I keep getting the following error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL

This is the code segment i'm attempting to use

<?php
$query = "SELECT * FROM song";
if (!($database = mysql_connect ("localhost","user","passwd")));

$row=mysql_fetch_array($result);
echo($row["song_title"]);
?>

I've removed all the spaces after ; as much as possible

Thanks in advance for any help.



Sun, 10 Jul 2005 08:22:11 GMT  
 Newbie Help with php connection to mysql

Quote:

> Warning: mysql_fetch_array(): supplied argument is not a valid MySQL

> This is the code segment i'm attempting to use

> <?php
> $query = "SELECT * FROM song";
> if (!($database = mysql_connect ("localhost","user","passwd")));

> $row=mysql_fetch_array($result);
> echo($row["song_title"]);
> ?>

> I've removed all the spaces after ; as much as possible

The spaces aren't the problem, the if/; are. Do yourself a favour and
write readable code, esp. when it's nested.

How php interpets your code:
<?php
if (!($database = mysql_connect ("localhost","user","passwd")))
{
   //do nothing on failure

Quote:
}


{
   //do nothing on failure

Quote:
}

$row=mysql_fetch_array($result);
echo($row["song_title"]);
?>

what you probably ment:

<?php
if ($database = mysql_connect ("localhost","user","passwd"))
{

   {
      $row=mysql_fetch_array($result);
      echo($row["song_title"]);
   }

Quote:
}

?>

--

  Daniel Tryba



Sun, 10 Jul 2005 08:37:35 GMT  
 Newbie Help with php connection to mysql
How about something more like this:

$host = "host_name";
$user = "user_name";
$pw = "u_pw";
$db = "db_name";

$link = mysql_connect($host, $user, $pw);
$sel = mysql_select_db($db);

$sql = "Select * From songs";
$q = mysql_query($sql, $link);
while($row = mysql_fetch_array($q)){
    echo $row['song_title'] . "<br>";

Quote:
}

$link = mysql_connect($host, $user, $pw);
$sel = mysql_select_db($db);

Quote:
> I'm attempting to use a basic script to connect to a mysql database
> and just print the first record to ensure the connections and
> everything are working. I keep getting the following error

> Warning: mysql_fetch_array(): supplied argument is not a valid MySQL

> This is the code segment i'm attempting to use

> <?php
> $query = "SELECT * FROM song";
> if (!($database = mysql_connect ("localhost","user","passwd")));

> $row=mysql_fetch_array($result);
> echo($row["song_title"]);
> ?>

> I've removed all the spaces after ; as much as possible

> Thanks in advance for any help.



Sun, 10 Jul 2005 18:32:27 GMT  
 Newbie Help with php connection to mysql


Quote:
> How about something more like this:

> $host = "host_name";
> $user = "user_name";
> $pw = "u_pw";
> $db = "db_name";

> $link = mysql_connect($host, $user, $pw);
> $sel = mysql_select_db($db);

> $sql = "Select * From songs";
> $q = mysql_query($sql, $link);
> while($row = mysql_fetch_array($q)){
>     echo $row['song_title'] . "<br>";
> }

or this

 $host = "host_name";
 $user = "user_name";
 $pw = "u_pw";
 $db = "db_name";

 $link = mysql_connect($host, $user, $pw) or
    die (mysql_error($link));
 $sel = mysql_select_db($db) or
    die (mysql_error($link));;

 $sql = "Select * From songs";
 $q = mysql_query($sql, $link)
 or
    die (mysql_error($q));;
   while($row = mysql_fetch_array($q)){
     echo $row['song_title'] . "<br>";
 }



Tue, 12 Jul 2005 01:00:54 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Newbie Prob with php connection to mysql

2. Newbie needs help setting up user MySQL thru PHP

3. PHP, mySQL and connection to a SQL Database

4. Passing MySQL connection parameters between PHP files

5. Newbie matter: MySQL connection

6. Newbie: apache+php+python+mysql installation.

7. Newbie Ques: PHP, MySQL & HTML Search Form

8. newbie: PHP/MYSQL

9. HELP: MySQL Connection

10. Help needed - mySQL connection problem

11. php mysql sample code php shareware like KB knowledgebase

12. PHP+MySQL vs. PHP+PostgreSQL

 

 
Powered by phpBB® Forum Software