multiple mysql_query requests in one document - php

I have a PHP document opening a connection to a database. Everything works fine - but as soon as I add one additional mysql_query request to the document, the mySQL server responds with a 500 internal server error.
Any idea why? Is there a reason that I cannot have multiple mysql_query requests in the one document?
thanks very much!
The offending PHP code is here (most of the echos are there for debugging reasons)
<?php
$q=$_GET["q"];
$con = mysql_connect('address.com', 'database', 'password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sql01_5789willgil", $con);
$sql1="SELECT * FROM place WHERE title = '".$q."'";
$result1 = mysql_query($sql1);
$row = mysql_fetch_array($result1);
$id=$row+1;
$sql2="SELECT * FROM place WHERE title = '".$q."'";
$result2 = mysql_query($sql2);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['latitude'] . "</td>";
echo "<td>" . $row['longitude'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "ID equals " . $id;
echo "row equals " . $row;
mysql_close($con);
?>

$sql1="SELECT * FROM place WHERE title = '".
mysql_real_escape_string($q, $con)."'"; <-- please escape
$result1 = mysql_query($sql1, $con); <-- always specify link identifier
if ($result1)
{
$row = mysql_fetch_array($result1); <-- always check result exist before fetch
}

From this here:
$sql1="SELECT * FROM place WHERE title = '".$q."'";
$result1 = mysql_query($sql1);
$row = mysql_fetch_array($result1);
$id=$row+1;
You're adding '1' to an array. I tried this script my box:
<?php
$row = array("my", "name", "is", "foo");
$id = $row + 1;
?>
and received this error:
Fatal error: Unsupported operand types
Perhaps this is the issue the script is complaining about.
From this answer here: Apache Fall Back When PHP Fails it very well be why it's returning a 500

Related

How do I display specific rows from a SQL database using ID as argument

I have php generated table that displays data as follow;
ID Name
1 xxxx
2 xxxx
I would like to be able to click on ID number and display information associated with the ID on separate page
Ive got so far:
table.php
include("connection.php");
$con=mysql_select_db('fm', $con);
$query = "SELECT * FROM table ;
$result = mysql_query($query);
echo "<table>
<tr>
<th>ID</th>
<th>Location</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td><a href='send.php?=" . $row['id'] . "'>" . $row['id'] . "</a></td><td>" . $row['location'] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>
info.php
include ("connection.php");
$con=mysql_select_db('fm', $con);
$id=$_GET['id'];
$query = "SELECT * FROM table WHERE id=". $id;
$result = mysql_query($query) or die (mysql_error());
echo "<table>
<tr>
<th>ID</th>
<th>Property</th>
<th>Location</th>
<tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['property'] . "</td></td>" . $row['location'] . "</td></tr>";
var_dump($row);
}
echo "</table>";
$result = mysql_query($sql);
mysql_close($con);
Any input will be appreciated
you have got a mistake here :
echo "<tr><td><a href='info.php?id=" . $row['id'] . "</td><td>" . $row['location'] . "</td></tr>";
you need to end the <a> element like this:
echo "<tr><td><a href='info.php?id=".$row['id']."'>".$row['id']."</a></td><td>" . $row['location'] . "</td></tr>";
Also you should use IF statement in the info.php because if you access it like :
info.php // without ?id=
you will have undefined variable $id. And its vulberable to mysql injection when you dont process the variable before using it in select.
You got mistake here in info.php in this line:
$result = mysql_query($query, $con);
it should look like this :
$result = mysql_query($query);
You got it right in table.php.
I would change the echo command like this
echo "<tr><td>{$row['id']}</td><td>{$row['property']}</td></td>{$row['location']}</td></tr>";
The double quotes will take care of the values of the variables.

mysqli_fetch_array keep getting error

$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','','my_db');
if (mysqli_connect_errno()) {
echo "Fail to connect :".mysqli_connect_error();
}
mysqli_select_db($con,"my_db");
$sql="SELECT * FROM ajax WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row('Firstname') . "</td>";
echo "<td>" . $row('Lastname') . "</td>";
echo "<td>" . $row('AGE') . "</td>";
echo "<td>" . $row('Hometown') . "</td>";
echo "<td>" . $row('Job') . "</td>";
echo "</tr>";
echo "</table>";
}
mysqli_close($con);
I keep getting this error :Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\abc\getuser.php on line 22
I apologize in advance if there is already an answer to this simple problem, I am new at coding and I have spent quite some time reading other similar questions but still couldn't solve this problem. Thx in advance.
As specified, the argument $result is boolean, not a mysqli_result.
In case of error in your SQL request, the function mysqli_query returns false.
You should see why their is a problem with your request by calling mysqli_error just after your mysqli_query if $result is false.
you could try
$sql=mysqli_query("SELECT * FROM ajax WHERE id = '".$q."' ");
echo "<table border='1'>
.......
while($row = mysqli_fetch_array($sql)) {

PHP mysql query returns a blank screen

I've commented out the bottom part, and the SQL query works fine. Its the displaying of the query where the error is coming from i believe.
$host = "127.0.0.1";
$user = "root";
$pass = "Toom13371!";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
// 2. Selecting DB.
$dbname = "filters";
mysql_select_db($dbname);
// 3. Build/Test SQL Query
$sql = ("select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'");
//echo $sql; //Comment/Uncomment to test sql query.
// 4. Retrieve info from MySQL.
$query = mysql_query($sql);
// 5. Display Query.
echo "<table border='1'>
<tr>
<th>Low Frequency</th>
<th>High Frequency</th>
</tr>";
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['Lowfreq'] . "</td>";
echo "<td>" . $row['Highfreq'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
Any help would be appreciated, I'm sure it's going to be some small stupid error i've over looked.
Thanks in advance :)
I'm guessing, based on your query, that you need to change this
mysql_select_db($dbname);
to
mysql_select_db($dbname, $connection);
and
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['Lowfreq'] . "</td>";
echo "<td>" . $row['Highfreq'] . "</td>";
echo "</tr>";
}
to
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['start_passband'] . "</td>";
echo "<td>" . $row['stop_passband'] . "</td>";
echo "</tr>";
}
Change line
mysql_select_db($dbname);
to
mysql_select_db($dbname, $connection);
Also before query check
$_POST['Lowfreq'] and $_POST['Highfreq']
If there is no value in these variables query will must return empty.
In your query there should not brackets for string.
$sql = ("select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'");
should be:
$sql = "select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'";

Getting data from database with php

I want to get the data from my database. The page does not change when I upload the file. Where am I wrong?
verifycheck.php
<?php
$con=mysql_connect ("###", "###", "###");
mysql_select_db ("db_name", $con);
$result = mysql_query($con,"SELECT * FROM db_tablename");
echo "<table border='1'>
<tr>
<th>username</th>
<th>email</th>
<th>password</th>
<th>confirm_password</th>
</tr>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['confirm_password'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Connection object should be second parameter and query string should be first parameter.
Try this
$result = mysql_query("SELECT * FROM db_tablename",$con);
Instead of
$result = mysql_query($con,"SELECT * FROM db_tablename");
Mysql function is deprecated and will remove in future go for Mysqli or PDO for preventing sql injection
Remove $con, from the mysql_query
As mysql_query() expects first parameter to be the SQL Query, not the SQL connection.
$result = mysql_query("SELECT * FROM db_tablename");
Please Check with your connection parameters as localhost,username password, database and also changing the
$result = mysql_query($con,"SELECT * FROM db_tablename");
as
$result = mysql_query("SELECT * FROM db_tablename",$con);

PHP search, show only session ID logs

I need to filter a session table, normally this is how I show the table list.
<?php
$con=mysqli_connect("localhost","root","","esc");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM sponsor ;");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>PIN</th>
<th>Author</th>
<th>Author ID</th>
<th>Entry Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['sid'] . "</td>";
echo "<td>" . $row['spin'] . "</td>";
echo "<td>" . $row['spuname'] . "</td>";
echo "<td>" . $row['suid'] . "</td>";
echo "<td>" . $row['sdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I was wondering if it's possible on this part...
$result = mysqli_query($con,"SELECT * FROM sponsor ;");
to add Where spuname = 'spuname'
I guess the way I'm doing it is wrong.
What I wanted to do is to show filtered tables, so the one that has the session will see only his logs.
mysqli_query("SELECT * FROM sponsor Where spuname = 'spuname'",$con);
try putting $con after your query
You can use where clause in your query. it would be
$result = mysqli_query($con,"SELECT * FROM sponsor Where spuname = 'spuname'");

Categories