php mysql query if $num_results > 0 - php

I want to make a php search query.
First put a sentence and explode every word get $name.
Then I put $name make a query to match all the name which exist in my database.
How to use a if ($num_results > 0) if I am not sure how many $name are matched and echo out?
(may be $row['name'] is null, maybe $row['name'] is 1 or 2, I want to get them one by one)
$query = mysql_query("SELECT * FROM books
WHERE name LIKE '$name[0]' OR name LIKE '$name[1]' OR name LIKE '$name[2]'
OR name like '$name[3]' ");
while ($row = mysql_fetch_array($query)) {
if ($num_results > 0) {
echo $row['name'][0] ;
}
if ($num_results > 1) {
echo $row['name'][1] ;
}
if ($num_results > 2) {
echo $row['name'][2] ;
}
if ($num_results > 3) {
echo $row['name'][3] ;
}
}

use:
mysql_num_rows($query);
to get the amount of rows you get from your mysql query
<?php if (mysql_num_rows($query)>0){?>
Do stuff
<? }else{ ?>
We didn't find anything!
<?php }?>

How about:
$i = 0
while($row = mysql_fetch_array($query)) {
echo $row['name'][$i];
$i++;
}

I didn't actually understood what you really want but your code should make more sense like this:
<?php
$searchphrase="alex nick george";
// this:
$names = explode(" ",$searchphrase);
// produces the following:
$names = array("alex","nick","george");
// so you can make the query:
if(is_array($names)){
$query = "SELECT * FROM books WHERE name IN ('".implode(",",$names)."') ORDER BY FIELD (name,".implode(",",$names).")";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) == 0){
// no results
}else{
while ($row = mysql_fetch_array($result)) {
echo $row['name'];
}
}
}
?>
Perhaps if you describe it better we can help more efficiently.

Related

Array value to string in PHP

I've got some code which randomly fetches data from a database, but whenever I prints the data in the browser is looks like that - ["data"]. I want it to be a clear string. How can I make that? Thank you!
Here is the code:
<?php
require '../wordbeater/adminpanel/includes/dbh.inc.php';
$result=mysqli_query($con, "SELECT * FROM users ORDER BY RAND() LIMIT 6");
$count = 0;
while($row=mysqli_fetch_row($result)){
$postsarray["one.$count."] = $row[1];
$postsarray["two.$count."] = $row[2];
$count++;
}
$encodedArray = array_map(utf8_encode, $postsarray);
echo $encodedArray;
mysqli_close($con);
?>
Try using:
$result=mysqli_query($con, "SELECT * FROM users ORDER BY RAND() LIMIT 6");
$count = 0;
while ($row = $result->fetch_row()) {
$postsarray["one.$count."] = $row[1];
$postsarray["two.$count."] = $row[2];
$count++;
}
I think you should go for mysqli_fetch_assoc as it will give you the same associative array as like your database.
for e.g.
while($row=mysqli_fetch_assoc($result)){
$postsarray["one.$count."] = $row["column_name_u_want_to_fetch"];
$postsarray["two.$count."] = $row["2_column_name_u_want_to_fetch"];
$count++;
}
You can directly store it in a variable so it will take it as a string.
for e.g.
$stringone="";
$stringtwo="";
while($row=mysqli_fetch_assoc($result)){
$stringone.$count = $row["column_name_u_want_to_fetch"];
$stringtwo.$count = $row["2_column_name_u_want_to_fetch"];
$count++;
}
for($i=0; $i<mysqli_num_rows($result); $i++)
{
echo $stringone.$i;
echo $stringtwo.$i;
}
try it by removing 'ORDER BY RAND() LIMIT 6'

IF-ELSE from MySQL based on single row

I try to get the value 'entryScans' from my SQL-table and send it to $output based on the IF-ELSE case. What am I doing wrong?
<?php
include ('config.php');
$TicketNo=$_POST["TicketNo"];
$hash=$_POST["hash"];
$sql = "SELECT TicketNo,hash,entryScans FROM `Tickets` WHERE `TicketNo` = '$TicketNo' AND `hash` = '$hash'" or die(mysql_error());
$result = mysql_query($sql);
$row=mysql_num_rows($result);
if($row['entryScans'] = 0){
$output="ok";
}
else if ($row['entryScans'] > 0) {
$output="maybe";
else{
$output="error";
}
print(json_encode($output));
mysql_close();
?>
You are doing ...
if($row['entryScans'] = 0){
Which has 2 problems, = is assignement, == is testing equal to. The second part is that your fetching the results in...
$result = mysql_query($sql);
$row=mysql_num_rows($result);
So $row is the number of rows, $result is the query results...
So to check the number of rows in the result set
if($row == 0){
etc.
Update:
If you want the value of the column entryScans to be used, then you need to change the call of mysql_num_rows() to mysql_fetch_assoc(), so
$row=mysql_fetch_assoc($result);
Then you can leave the rest of the code to use $row['entryScans']
You are not processing your results. You just get the total number of rows in $row variable.
You need to process your query result by looping method.
Here is the example
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
if($row['entryScans'] = 0)
{
$output="ok";
}
else if ($row['entryScans'] > 0)
{
$output="maybe";
}
else
{
$output="error";
}
}
Hope this works for you.

query select * from table limit 0, 100, for every (even limit) echo something

i have a database wherein i echo anything i want but i want to echo something else every time the code echos 2 things. I actually tried foreach() function but im actually quite confused on how to use it with this kind of situation
here's an example:
$sql = "SELECT * FROM table ORDER BY ID DESC LIMIT 0, 100";
$rs = mysql_query($android_app_sort,$con);
while($row = mysql_fetch_assoc($android_app_q_sort))
{
echo $row['ID'];
<--FOR EVERY 2 echos's then ECHO SOMETHING-->
}
all help is appreciated
Like this:
for( $i = 0; $row = mysql_fetch_assoc($android_app_q_sort); $i++ ) {
if( ( $i % 2 ) == 0 ) {
print "Even row."
}
}
$sql = "SELECT * FROM table ORDER BY ID DESC LIMIT 0, 100";
$rs = mysql_query($android_app_sort,$con);
$iterator = 0;
while($row = mysql_fetch_assoc($android_app_q_sort))
{
$iterator++;
if($iterator%3 == 0) {echo 'something';}
echo $row['ID'];
}
use simple modulo operation, this will print every third row something.
You can use like below code
$i=0;
while($row = mysql_fetch_assoc($android_app_q_sort))
{
if($i%2 == 0)
{
echo $row['ID'];
}
$i++;
}

Fetch all fields from all rows - MYSQL

I understand this could appear alarming but bear with me.
I need to echo every field in every row in a table.
This is only an example - I have removed the HTML wrapped around it to improve readability.
$a = 1;
while ($a <= $count_rows) {
$query = "SELECT col1, col2 etc.. FROM table WHERE `id`='$id'";
$result = mysqli_query($con, $query);
$i = 1;
while($i <= $count_fields) {
$row = mysqli_fetch_array($result, MYSQL_NUM);
echo "$row[$i]";
$i++;
}
$a++;
$id = $a;
}
This only outputs the first field of every row? Why?
If I echo $row[2] I get nothing!
If I echo $row[2] I get nothing!
because it's actually third item
and there is some strange code interfering with $i variable
Anyway, to get every column from every row ou need a code like this
$query = "SELECT * FROM table";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_row($result)) {
foreach ($row as $index => $value) {
echo "$index => $value, ";
}
echo "<br>\n";
}

MySQL query limit part of query

I have a mysql query:
$result = mysql_query("SELECT * FROM $table WHERE cat = 'category'");
while($row = mysql_fetch_array($result)) {
echo '
<hgroup><h3>'.$row['mag'].'</h3><h4>'.$row['date'].'</h4></hgroup>
'.$row['title'].'
';
}
This query will generally select between 2 and 5 different rows and display them in a list.
I want the first echoed line to only appear once and the second line should appear between 2 and 5 depending on the data in my db.
I am sure there is a simple way to do this, I've tried GROUP BY mag but this will eliminate the remaining 1-4 pieces of data I wish to display.
Not sure I understand your question, as the following solution seems too simple!
$row = mysql_fetch_array($result);
echo '<hgroup><h3>'.$row['mag'].'</h3><h4>'.$row['date'].'</h4></hgroup>
'.$row['title'].'';
while ($row = mysql_fetch_array($result)) {
echo ''.$row['title'].'';
}
May be this is a solution to your problem ?
$lines = '';
unset($hgroup);
$result = mysql_query("SELECT * FROM $table WHERE cat = 'category'");
while($row = mysql_fetch_array($result)) {
if (!isset($hgroup)) {
$hgroup = '<hgroup><h3>'.$row['mag'].'</h3><h4>'.$row['date'].'</h4></hgroup>';
}
$lines += '
'.$row['title'].'
';
}
echo $hgroup.$lines;

Categories