Forum Moderators: coopster
if ( isset($_GET['show_num']) and ctype_digit($_GET['show_num']) )
$show_num = $_GET['show_num'];
else
$show_num = -1;
$sql = 'SELECT * FROM `Show_Data` WHERE 1 AND `show_num` = $show_num LIMIT 0, 30';
**(obviously the database is Show_Data and show_num is the number to identify each show).**
So now when you type in show.php?show_num=1 it creates a variable $show_num and the page "gets it" and selects the row that has $show_num of (some number).
What I want to happen next is to be able to use all the other data the page selects from the database; it selected everything and I want to be able to display it in different parts of the page. I have show_bio, show_name, and show_station in that row, but I have not idea how to display that information for that row! Please help me.
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("database",$db); Then execute the sql statement:
$result = mysql_query($sql); This gets the data and puts it in an array called myrow:
$myrow = mysql_fetch_array($result); Now you can reference your data with the following variables:
$myrow["show_bio"]
$myrow["show_name"]
$myrow["show_station"]
The above assumes you want and expect only a single row. You can keep calling
$myrow = mysql_fetch_array($result); to get the other rows, best done in a while loop. Check out the MySQL Functions [php.net] doc as well where you'll see all your options and methods for error checking.
Once you get this working, be sure to check out the extract function [php.net] which stores values from a query into easier to use variable names.
<?php
include 'Includes/dbConnect.php';
if ( isset($_GET['show_num']) and ctype_digit($_GET['show_num']) )
$show_num = $_GET['show_num'];
else
$show_num = -1;
$sql = 'SELECT * FROM `Show_Data` WHERE 1 AND `show_num` = $show_num LIMIT 0, 30';
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);?>
And the dbConnet.php code is
<?php
$conn = mysql_connect("localhost", "username", "password");
mysql_select_db ("newbobbo_Sitemain");
?>
I get this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/newbobbo/public_html/Showstemplatephp.php on line 10
Line 10 code is $myrow = mysql_fetch_array($result);?>
print out your $sql variable to make sure your SQL statement's good:
echo "<br>$sql<br>";
If it's not immediately apparent, execute that query in PHPmyAdmin or command-line mysql to see what happens with it.
I changed my select statement to $result = mysql_query("SELECT * FROM `Show_Data` WHERE 1 AND `show_num` = $show_num LIMIT 0, 30");
and all my code so far is
<?php
include 'Includes/dbConnect.php';
if ( isset($_GET['show_num']) and ctype_digit($_GET['show_num']) )
$show_num = $_GET['show_num'];
else
$show_num = -1;
$result = mysql_query("SELECT * FROM `Show_Data` WHERE 1 AND `show_num` = $show_num LIMIT 0, 30");
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);?>
but I now have two $result variables, is that a problem, how should I deal with it?
Why the WHERE 1?
It almost looks like you are trying to test the validity of $show_num, but I can't really tell from this code snippet. If that is the case why not:
else
exit;
instead of:
else
$show_num = -1;
Alternatively you could set $show_num to false and test this way
if ( isset($_GET['show_num']) and ctype_digit($_GET['show_num']) )
$show_num = $_GET['show_num'];
else
$show_num = false;
if($show_num)
{
$sql = 'SELECT * FROM `Show_Data` WHERE `show_num` = $show_num LIMIT 0, 30';
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
}
else
echo "That is not a valid show...";
?>
echo your $result and $myrow. I'm guessing that you are passing mysql_fetch_array an empty variable.
WBF
It just echos back
SELECT * FROM `Show_Data` WHERE 1 AND `show_num` = $show_num LIMIT 0, 30
That's because you were using single quotes around the string defining the sql. Use double quotes so your variable gets replaced properly. Single quotes are literal.
You solved that by putting the sql in double quotes right in the mysql_query statement.
but I now have two $result variables, is that a problem, how should I deal with it?
All you really need is this:
$sql = "SELECT * FROM Show_Data WHERE show_num = $show_num"; // note the double quotes
$result = mysql_query($sql); and get rid of the other $result=... line.
I also got rid of the backquotes, the '1 and', and the LIMIT statement at the end. Looks like you're getting this out of PHPmyAdmin. You don't need those.
[edited by: bonanza at 12:10 am (utc) on May 4, 2003]
<html>
<?php
$conn = mysql_connect("localhost", "username", "pw");
$db= mysql_select_db("newbobbo_Sitemain");
$show_num = $_GET['show_num'];
$sql = "SELECT * FROM Show_Data WHERE show_num = $show_num";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);?>
And yet I ended up with this error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/newbobbo/public_html/shows1.php on line 14
BTW, I thank all of you for helping me out, this is kind of difficult for me so I thank you.
Is show_num a numeric field? If not, you need to surround $show_num with single quotes in the sql statement.