MULTI SEARCH PHP - php

Can anyone help me to figure out the error in my code? i want to display data through two input search..my code goes like these.
<table>
<tr>
<td>Studid</td>
<td>Course</td>
</tr>
<?php
include ("connect.php");
if(isset($_POST['submit']))
{
$studno=$_POST['idsearch'];
$scourse=$_POST['coursesearch'];
$sql=mysql_query("SELECT * FROM cfnr WHERE studid= ".$studno." AND course=".$scourse."");
}
?>
<?php
while($row=mysql_fetch_array($sql))
{
}
?>
<tr>
<td><?php echo $row['studid'];?></td>
<td><?php echo $row['course'];?></td>
</tr>
</table>
i got this error in my screen.
"mysql_fetch_array() expects parameter 1 to be resource, boolean given in "
thanks! :)

You should use a bit of debugging. Try this:
$sql = "SELECT * FROM cfnr WHERE studid= '".$studno."' AND course = '".$scourse."'";
$query = mysql_query($sql) or die(__LINE__."has an error: ".mysql_error()); // Gives an error if there's a syntax error
$row = mysql_fetch_array($query);
echo "<pre>";
print_r($row);
exit;
while ($row = mysql_fetch_array($query)) {
/* Other code */
}

Replace your sql with below line :
$sql=mysql_query("SELECT * FROM cfnr WHERE studid= ".$studno." AND course=".$scourse."") or die(mysql_error());

The issue is your `$sql variable is within the if condition and if your if condition fails there is no scope of $sql outside of if condition
Instead of
while($row=mysql_fetch_array($sql))
Use
while($row=#mysql_fetch_array($sql))
you will not get this error.

("SELECT * FROM cfnr WHERE studid= '$studno' AND course ='$scourse.'") or die(mysql_error());

Change the code :
while($row=mysql_fetch_array($sql))
to
while($row=mysql_fetch_assoc($sql))
and also change this code:
$sql=mysql_query("SELECT * FROM cfnr WHERE studid= ".$studno." AND course=".$scourse."");
to
$sql=mysql_query("SELECT * FROM cfnr WHERE studid= '$studno' AND course='$scourse'");
Not only that, the close brace of the isset() should be on before your (?>) last closing php tag.
Change the code :
include ("connect.php");
to
include 'connect.php';

Related

How can I add an Sql Query in the middle of HTML code?

I want to make a login system, where the user can see all the messages he has received. In my database the insurance_company is the username and that is what I want it to show by. Here is my code. How do I fix it?
- Thanks Prem
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Claim'] . "<br />";
}
?>
PHP code can be written anywhere in HTML code. You need to just place your PHP code between tags. As you can see in the example bellow I have used php code between p tags.
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {?>
<p> <?php echo $row['Claim'] . "<br />"; ?> <p>
<?php }
?>
If you want it to show insurance_company, you must echo this value in the response array:
echo $row['insurance_company']

php error - Undefined index: id

I have got an error message in php like this "undefined index id in line no:", I have using the following code
$id='$_REQUEST[id]';
$sql = "SELECT * FROM country";
$result = $con->query($sql);
$i=1;
foreach($result as $row)
{
?>
<li><?php echo $row['name'] ?> </li>
<?php
$i++;
}
?>
For listing the following code i have used
<?php
if(isset($id)) {
$queryImg = "SELECT * FROM data WHERE country='$_REQUEST[id]'";
$resultImg = mysqli_query($con,$queryImg);
$rowResult = mysqli_num_rows($resultImg);
while($rowsImg = mysqli_fetch_array($resultImg)){ ?>
I do not have enough experience in php, so can you please check this code and tell me how to solve this.
remove single quotes like $id=$_REQUEST['id'];
and here $queryImg = "SELECT * FROM data WHERE country='".$_REQUEST['id']."'";
Just remove the quote from the variable $id, like this:
$id=$_REQUEST['id'];

Printing mysqli query results

i am trying to print the results of a php query my php code is:
<?php
include 'header.php';
include 'conect.php';
$resultlog = mysqli_query("SELECT * from cpi ,$con);
while($row = mysql_fetch_array($resultlog))
print $row;
mysqli_close($con);
?>
but it result an error
Parse error: syntax error, unexpected end of file
Firstly, the DB connection comes first in mysqli plus, there's a missing quote.
You're also mixing APIs.
Then add the proper bracing.
$resultlog = mysqli_query($con,"SELECT * from cpi") or die(mysqli_error($con));
while($row = mysqli_fetch_array($resultlog)){
print $row;
}
mysqli_close($con);
Make sure your DB connection which is not shown, is in fact mysqli and not mysql, nor PDO.
None of those APIs intermix.
However, just doing a print $row may probably not show you what you like to get.
Therefore, you may need to elaborate on that.
You're probably wanting to do something like:
echo $row['your_column_name'].'<br />';
or as Ghost stated:
print $row[0]; or print $row['column_name']
"Its working fine. But can we print all result through one command?"
Yes, like this:
$resultlog = mysqli_query($con,"SELECT * from cpi") or die(mysqli_error($con));
$row = mysqli_fetch_array($resultlog);
foreach($row as $r) {
echo $r . "<br>";
}
try this:
<?php
include 'header.php';
include 'conect.php';
$resultArray = array();
$resultlog = mysqli_query($con, "SELECT * from cpi");
while($row = mysqli_fetch_array($resultlog)){
$resultArray[] = $row;
}
mysqli_close($con);
print_r($resultArray);
?>

MySQL data does not appear in tooltip

I want to show MySQL data inside a tooltip.
Here is the code:
<?php
$abfrage = "SELECT * FROM tester";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis)){
$Name=$row->Name;
$Bech=$row->Beschreibung;
}
?>
Test Link
It shows me an empty tooltip with nothing inside. If I print those two variables, the MySQL data appear. Is there any mistake in the code?
Thanks.
This is in conjunction with Seth McClaine's answer.
Echo your values using:
<?php echo $Name; ?> <?php echo $Bech; ?>
Instead of <?=$Name?> <?=$Bech?>
The use of short tags is not recommended for something like this.
Reformatted code:
<?php
$abfrage = "SELECT * FROM tester";
$ergebnis = mysql_query($abfrage);
$row = mysql_fetch_object($ergebnis);
$Name=$row->Name;
$Bech=$row->Beschreibung;
?>
Test Link
If you just want the first result remove the while...
<?php
$abfrage = "SELECT * FROM tester";
$ergebnis = mysql_query($abfrage);
$row = mysql_fetch_object($ergebnis);
$Name=$row->Name;
$Bech=$row->Beschreibung;
?>
Test Link

Warning: unexpected character in input: " (ascii=29) state=0 in

I've been reading about what others have done with this error and have made changes to my php.ini file, added code to override another php setting, and still end up with this same error. Here is my code:
<html>
<body>
<table>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php
function getRecords($query) {
$con = mysql_connect("localhost", "movie", "moviepw");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("movies", $con);
$result = mysql_query($query);
// THE ERROR IS REPORTED ON THIS LINE
return $result;
}
function buildQuery() {
$keyword = $_GET['keyword'];
$sql = "SELECT * from movies WHERE
(
'movie_title' LIKE '%keyword%'
OR
'movie_description' LIKE '%keyword%'
)";
return $sql;
}
$query = buildQuery();
$records = getRecords($query);
while($row = mysql_fetch_array($records)){ ?>
<tbody>
<table border='1'>
<tr>
<td><?= $row['movie_title']; ?></td>
<td><?= $row['movie_rating']; ?></td>
<td> <img src="<?= $row['movie_image'];?>"> </td>
<td><?= $row['movie_description']; ?></td>
<td>Return to Search</td>
</tr>
<? } ?>
</tbody>
</table>
</body>
</html>
Any idea why I'm getting this error?
The editor had added spaces that were not deletable. I had to delete several lines and rewrite them. So, this issue wasn't exactly with the code...just a text editor software problem.
The other error I had was a boolean error with my query. Turns out I was trying to query the database instead of the table.
Thanks for all the help with this!
Remove the single quotes from the column names in your query. This may not be the only error, if the PHP interpreter is still complaining about ASCII 29.
$sql = "SELECT * from movies WHERE
(
'movie_title' LIKE '%keyword%'
OR
'movie_description' LIKE '%keyword%'
)";
// Should be
$sql = "SELECT * from movies WHERE
(
movie_title LIKE '%keyword%'
OR
movie_description LIKE '%keyword%'
)";

Categories