Select mysql in PHP with 2 vars - php

How i can make this whitout php notice error:
$id = $_GET['id'];
$name = $_POST['name'];
$sql = mysql_query("SELECT * FROM table WHERE id = '$id' or name = '$name');
And how i can do one search in many tables at once.
Thank you

Your query string is incomplete. You need a closing double quote at the end.
$sql = mysql_query("SELECT * FROM table WHERE id = '$id' or name = '$name'");
^ ^
To search many tables at once, add more tables separated by a comma.
"SELECT table1.field, table2.field FROM table1, table2"

$id = isset($_GET['id']) ? $_GET['id'] : null;
$name = isset($_POST['name']) ? $_POST['name'] : null;
if($id !== null || $name !== null) {
$sql = mysql_query("SELECT * FROM table WHERE id = '$id' or name = '$name'");
}

Related

php file get the last id from mysql

I try to get the title and mess in last id5, this is the result.
php code :
<?php
$hostname_mus_db = "localhost";
$database_mus_db = "message";
$username_mus_db = "root";
$password_mus_db = "";
$tf=#mysql_connect($hostname_mus_db,$username_mus_db,$password_mus_db);
if(!$tf)
die('Connection problem1 ..');
$tf_db=mysql_select_db($database_mus_db);
if(!$tf_db)
{
#mysql_close($tf_handle);
die('selection problem2 ..');
}
$co_array=array();
//if (isset($_GET['co'])) {
// $co=$_GET['co'];
$sql = mysql_query("SELECT * FROM mha1 where id>'1'"); /// here i edit the variable
while($row = mysql_fetch_assoc($sql)){
$co_array[]=$row;
}
// }
header('Content-Type:application/json');
echo json_encode(array("date"=>$co_array));
?>
in first the code is :
<?php
$hostname_mus_db = "localhost";
$database_mus_db = "message";
$username_mus_db = "root";
$password_mus_db = "";
$tf=#mysql_connect($hostname_mus_db,$username_mus_db,$password_mus_db);
if(!$tf)
die('Connection problem1 ..');
$tf_db=mysql_select_db($database_mus_db);
if(!$tf_db)
{
#mysql_close($tf_handle);
die('selection problem2 ..');
}
$co_array=array();
if (isset($_GET['co'])) {
$co=$_GET['co'];
$sql = mysql_query("SELECT * FROM mha1 where id>".$co);
while($row = mysql_fetch_assoc($sql)){
$co_array[]=$row;
}
}
header('Content-Type:application/json');
echo json_encode(array("date"=>$co_array));
?>
but the result was
How can I fix it ?? how can I make the result is just id 5 not all id.
I hope i understand your question correctly.
If you want to know the last entry in a table and id is your primary key, use DESCENDING ORDER AND LIMIT
$sql = mysql_query("SELECT id FROM mha1 ORDER BY id DESC LIMIT 1");
If you insert a new dataset and want to know the last inserted id you can use insert_id
$sql = mysql_query("INSERT INTO mha1 (id) VALUES ('')");
$last_id=mysql_insert_id();
You can just get all the results with this query in descending order then last inserted id is on top then fetch eaisly.
$sql = mysql_query("SELECT * FROM tablename ORDER BY id DESC limit 0,1 ");
Please just change this part:
$sql = mysql_query("SELECT * FROM mha1 where id>".$co);
to this:
$sql = mysql_query("SELECT * FROM mha1 where id=".$co);
then $co can be an value of id you want.

PHP Fetch All Data Only 1 No

I want to fetch all users but not this user (id = 12) how to make this ?
$MyID = '12';
$sql_query = mysqli_query($Conn, "SELECT * FROM users WHERE country='$MyCountry'");
while ($fetch_data = mysqli_fetch_array($sql_query)) {
$firstname = $fetch_data['firstname'];
echo $firstname;
}
Well in it's simplest form:
$sql_query = mysqli_query($Conn, "SELECT * FROM users WHERE country='$MyCountry' and userid <> $MyID");
But this is inadvisable because your values are not being escaped properly. Better to use prepared statements or mysqli_real_escape_string
$stmt = $mysqli->prepare("SELECT * FROM users WHERE country = ? and userId <> ?")
$stmt->bind_param("sd",$MyCountry, $MyId);
$stmt->execute();
select * from user WHERE ID!=$mycountry;
You can do it easily like that
lets say your field is an id = 12
then
SELECT * FROM your_table WHERE id <> 12

How to loop php using ref data from mysql?

How to loop php using ref data from mysql ?
work flow :
Loop If column 'xxx' == ''
{
$sql = "SELECT * FROM table WHERE xxx == '' order by id desc";
$result = mysql_query($sql);
$datas=mysql_fetch_array($result);{
$id = stripslashes(str_replace('\r\n', '<br>',($datas['id'])));
}
$sql = "UPDATE table SET xxx = 'test' WHERE id = '$id' ";
$dbQuery = mysql_query($sql);'
}
ypur query is confusing. why would you store ids with html in them? regardless, this should work
$sql = "SELECT * FROM table WHERE xxx != '' "; //ORDER BY will not work since ids are not int
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result){
$id = stripslashes(str_replace('\r\n', '<br>',($row['id'])));
$sql = "UPDATE table SET xxx = 'test' WHERE id = '$id' ";
$dbQuery = mysql_query($sql);'
}

Solve with only one query?

I have the following table:
CREATE TABLE list(
country TINYINT UNSIGNED NOT NULL,
name VARCHAR(10) CHARACTER SET latin1 NOT NULL,
name_index INT UNSIGNED NOT NULL,
UNIQUE KEY(country, name), PRIMARY KEY(country, name_index)) ENGINE = INNODB
I want to:
Given: ($country, $name, $new_index)
Check if a row with country = $country && name = $name exists.
If the row exists, get the index $index = name_index.
If the row doesn't exist, add it and then get the index.
I can do the following using many queries, but I am looking for an efficient way to do it, using only one query. Is this possible?
It's not possible with only one query.
You CAN do this:
$sql = "SELECT name_index FROM (your table) WHERE country = '$country' AND
name = '$name' LIMIT 1";
$query = mysql_query($sql);
$numrows = mysql_num_rows($query);
if($numrows == 1) {
$row = mysql_fetch_row($query);
$index = $row[0];
} else {
$sql = "INSERT INTO (your table) (country, name)
VALUES('$country','$name')";
$query = mysql_query($sql);
$check = mysql_num_rows($query);
if($check > 0) {
$sql = "SELECT name_index FROM (your table) WHERE country = '$country' AND
name = '$name' LIMIT 1";
$query = mysql_query($sql);
$row = mysql_fetch_row($query);
$index = $row[0];
} else {
echo "Error occured while trying to insert new row";
}
}
Hope this helps :).

SQL Update ceasing to work?

I want to update a mysql database. That has become a common practice for me, but for some reason with no error, it just doesn't work. The only thing I have never done is compare against 2 variables(in this case, ID && Name)
$name = $_POST['name'];
$duty = $_POST['duty'];
$number = $_POST['number'];
$url = $_POST['url'];
$insert = "UPDATE vendors SET name = '$_POST[name]', duty = '$_POST[duty]', number = '$_POST[number]', url = '$_POST[url]' WHERE id = '$id' && name = '$name'";
$result=mysql_query($insert) or die(mysql_error());
if ($result) {
header("location:**HIDDEN**");
Any help would be appreciated.
Instead of &&, you should use AND to add another where-condition.
Write this instead:
$name = $_POST['name'];
$duty = $_POST['duty'];
$number = $_POST['number'];
$url = $_POST['url'];
$insert = "UPDATE `vendors` SET `name` = '{$_POST['name']}', `duty` = '{$_POST['duty']}', `number` = '{$_POST['number']}', `url` = '{$_POST['url']}' WHERE (`id` = '$id' AND `name` = '$name')";
$result = #mysql_query($insert) or die(mysql_error());
header("location:**HIDDEN**");
It should now work. Notify me if there still is a problem.
replace && with AND and you should be good
Your Query is wrong. Following is the correct one.
The way you have used the variables is wrong.
You had not written any code for $id. What is that?
$insert = "UPDATE vendors SET name = '".$_POST['name']."', duty = '".$_POST['duty']."', number = '".$_POST['number']."', url = '".$_POST['url']."' WHERE id = '$id' AND name = '$name'";

Categories