the code i have below basically behaves as a friend search engine where you can add someone if you don't have them on you account. So i am trying to do the following: If exists it should just display the friend and if not then it should say add friend.
I got it to work but the tail end of the query (please see this comment "//Problem code - when i use this echo below this where it repeats 3 times") is giving me trouble where it repeats Add 3 times.
<?php
$num_rows1 = mysql_num_rows($result);
if ($result == "") {
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if ($rows == 0) {
print("<div id=norequests>No results for <strong>$q
</strong></div>");
}
elseif ($rows > 0) {
while ($row = mysql_fetch_array($query))
{
$person = htmlspecialchars($row['full_name']);
$linksys = htmlspecialchars($row['name']);
$pid = htmlspecialchars($row['system_id']);
}
print("");
}
}
else{
echo '<div id="error">No results.</div>';
}
$sql = "SELECT `Friend_id` from `friends_container`
WHERE `System_id` = '$sid'";
$result = mysql_query($sql);
$query = mysql_query($sql) or die("Error: " . mysql_error());
if ($result == "") {
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if ($rows == 0) {
print("");
}
elseif ($rows > 0)
{
while ($row = mysql_fetch_array($query))
{
$existing = htmlspecialchars($row['Friend_id']);
if ($existing == $pid) {
echo("<img src=$linksys />$person - Already Existing");
}
else
//Problem code - when i use this echo below this where it repeats 3 times
{
echo("Add $person");
}
}
?>
You must have 3 images stored for each account.
Your query will always result in a multiple result. what you should do is use php to convert it to something which will result in a better option or say convert the result in an array for convenience.
Related
I have a C# code and a PHP that gets info I send from C#.
So I need to have such code:
<?php
$link = mysqli_connect('localhost','root','');
$database = mysqli_select_db($link,'serial');
$serial = $_GET['serial'];
$hwid = $_GET['hwidin'];
$sql = "SELECT * FROM serial WHERE serial = '". mysqli_real_escape_string($link,$serial) ."'" ;
$result = $link->query($sql);
/*
0 = Wrong HWID
1 = HWID is correct
2 = HWID left empty
3 = No serial with that key
*/
if(strlen($hwid) < 1)
{
echo "2";
}
else
{
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if (strlen($row['hwid']) > 1)
{
if ($hwid != $row['hwid'])
{
echo "0";
}
else
{
echo "1";
}
}
else
{
$sql = "UPDATE serial SET hwid='$hwid' WHERE serial='$serial'";
echo "1";
if(mysqli_query($link, $sql))
{
echo $row['hwid'];
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
}
}
else
{
echo "3";
}
}
So what I want to do is add some checking, for example:
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if (strlen($row['hwid']) > 1)
{
if ($hwid != $row['hwid'])
{
echo "0";
}
else
{
echo "1";
}
If echo "1" {
$sql = "SELECT time FROM serial Where serial = '". mysqli_real_escape_string($link,$serial) ."'" ;
$result = $link->query($sql);
if $result > 1 {
echo "55";
}
I need somehow to combine it with hwid checker and serial checker.
I want to check "time" column variable and use > < 1 to decide what to echo.
hi guys im trying to insert a mysql data to a variable that will set an if condition depending on the result. is this possible, am i doing it right? what is the right way to do it ? what i want to achieve is to validate if there's a equal value given by the user inside my mysql rows.
$db = mysql_connect('localhost','test','');
if (!$db)
{
print "<h1>Unable to Connect to MySQL</h1>";
}
$dbname = 'test';
$btest = mysql_select_db($dbname);
if (!$btest)
{
print "<h1>Unable to Select the Database</h1>";
}
$sql_statement = "SELECT * ";
$sql_statement .= "FROM registered_email ";
$result = mysql_query($sql_statement);
$outputDisplay = "";
$myrowcount = 0;
if (!$result) {
$outputDisplay .= "<br /><font color=red>MySQL No: ".mysql_errno();
$outputDisplay .= "<br />MySQL Error: ".mysql_error();
$outputDisplay .= "<br />SQL Statement: ".$sql_statement;
$outputDisplay .= "<br />MySQL Affected Rows: ".mysql_affected_rows()."</font><br />";
}
else{
$numresults = mysql_num_rows($result);
for ($i = 0; $i < $numresults; $i++)
{
$row = mysql_fetch_array($result);
$id = $row['id'];
$sentEmailClients = $row['email'];
$outputDisplay.= "".$sentEmailClients."<br />";
}
}
and here what im trying to achieve, btw is $clientEmail has a default values so dont worry about that.
if($clientEmail === $outputDisplay){
...... some codes..........
}
else{
....... some codes.......
}
you can use mysql row to compare with your user input. you can add condition, while you'r getting row value for the email inside the loop.
$email_exist = 0;//define the default value.
for ($i = 0; $i < $numresults; $i++)
{
$row = mysql_fetch_array($result);
$id = $row['id'];
$sentEmailClients = $row['email'];
$outputDisplay.= "".$sentEmailClients."<br />";
//my code start here
if($sentEmailClients == $clientEmail)
$email_exist = 1;
}
//outside the loop
if($email_exist == 1) {
//..........write some code.......
}else{
//........write some code.......
}
why don't you use a while loop?
make sure to update to mysqli_* because mysql_* is deprecated and is going to get removed on php 7.0
$email_exist = 0;//define the default value.
while ( $row = mysql_fetch_assoc($result) ) // you are using associative array and not the indexed once tho you should go for mysql_fetch_assoc
{
$id = $row['id'];
$sentEmailClients = $row['email'];
$outputDisplay.= "".$sentEmailClients."<br />";
//my code start here
if($sentEmailClients == $clientEmail)
$email_exist += 1; //maybe it exist more than once?
}
//outside the loop
if($email_exist == 1) {
//..........write some code.......
}else{
//........write some code.......
}
or you can do something more simple like this
$query = "select email from tablename where email='$clientemail'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count > 0) {
// email exists
} else {
// doesn't exist
}
I am a completely newbie in programming php I would like to make this code below return many arrays(to flash as3), however I only receive one array.Can anyone please pinpoint what is my mistake here? thanks.
$data_array = "";
$i = 0;
//if(isset($_POST['myrequest']) && $_POST['myrequest'] == "get_characters")
//{
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql))
{
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1)
{
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
else
{
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
echo "returnStr=$data_array";
exit();
}
When you write your exit insight your loop you stop executing your program and you get only one record. You should set the echo and exit after your while loop.
$data_array = "";
$i = 0;
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql)) {
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1) {
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
} else {
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
}
echo "returnStr=$data_array";
exit();
Those two last line of your should be outside of your loop:
$data_array = "";
$i = 0;
//if(isset($_POST['myrequest']) && $_POST['myrequest'] == "get_characters")
//{
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql))
{
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1)
{
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
else
{
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
}
echo "returnStr=$data_array";
exit();
If you would name the columns that you want in the SELECT then it's much simpler. Make sure to use MYSQLI_ASSOC in the fetch:
$sql = mysqli_query($conn, "SELECT Username, Fb_id, Access_token, Fb_sig, Char_id FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql, MYSQLI_ASSOC))
{
$data_array[] = implode('|', $row);
}
echo "returnStr=" . implode('(||)', $data_array);
exit();
The query I have below will only show me one result even if there are multiple matching entries (completely or partially matching). How do I fix it so it will return all matching entries:
//$allowed is a variable from database.
$sql = "SELECT `users`.`full_name`, `taglines`.`name`, `users`.`user_id` FROM
`users` LEFT JOIN `taglines` ON `users`.`user_id` = `taglines`.`person_id`
WHERE ( `users`.`user_settings` = '$allowed' ) and ( `users`.`full_name`
LIKE '%$q%' ) LIMIT $startrow, 15";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$num_rows1 = mysql_num_rows($result);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$person = htmlspecialchars($row['full_name']);
}
}
}
print $person;
Because your overwriting $person on each iteration.
Hold it in a $person[] array if your expecting more then one. Then loop through it with a foreach loop when you intend to output.
Not related but your also querying twice, you only need 1 $result = mysql_query($sql);
Update (Simple Outputting Example):
<?php
$person=array();
while($row = mysql_fetch_array($query)){
$person[] = array('full_name'=>$row['full_name'],
'email'=>$row['email'],
'somthing_else1'=>$row['some_other_column']);
}
//Then when you want to output:
foreach($person as $value){
echo '<p>Name:'.htmlentities($value['full_name']).'</p>';
echo '<p>Eamil:'.htmlentities($value['email']).'</p>';
echo '<p>FooBar:'.htmlentities($value['somthing_else1']).'</p>';
}
?>
Or an alternative way to is to build your output within the loop using concatenation.
<?php
$person='';
while($row = mysql_fetch_array($query)){
$person .= '<p>Name:'.$row['full_name'].'</p>';
$person .= '<p>Email:'.$row['email'].'</p>';
}
echo $person;
?>
Or just echo it.
<?php
while($row = mysql_fetch_array($query)){
echo '<p>Name:'.$row['full_name'].'</p>';
echo '<p>Email:'.$row['email'].'</p>';
}
?>
I am using the code below to select items and using the $result variable to do a count. if there are less than 1 it will say add more and if there are more than 5 it will say view all. It works for less than 1 but it won't for more than 5. Am i doing it right?
//Query
$sql = "SELECT id, name, why, date_time
FROM tabs
WHERE p_id = '$pid'
ORDER BY id
LIMIT 0, 5";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
if ($result == "") {
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0) {
print("");
} elseif($rows > 0) {
while($row = mysql_fetch_array($query)) {
$name = $row['name'];
$w = nl2br($row['why']);
$y = $row['date_time'];
print("echoing contents here");
}
}
if(mysql_num_rows($result) > 5) {
echo "view all";
}
if(mysql_num_rows($result) < 1) {
echo "add one";
} ?>
if(mysql_num_rows($result) > 5) {
echo "view all";
}
if(mysql_num_rows($result) < 1) {
echo "add one";
}
Should be
if($rows > 5) {
echo "view all";
}
if($rows < 1) {
echo "add one";
}
Since you exhausted the result set with the previous mysql_fetch_array() loop