HI, I am trying the below code .
$domain = $_GET['url'];
$sql = 'SELECT * FROM `domains` WHERE `domain` REGEXP CONVERT(_utf8 \'$url\' USING latin1) COLLATE latin1_swedish_ci';
$result = mysql_query($sql);
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
echo "id :{$row[0]} <br>";
}
will get the domain from user using $_GET and then regex that from database ..
This query is not working , please tell me the proper syntax
I even tried double quotes \"$domain"\
$sql = "SELECT * FROM `domains` WHERE `domain` REGEXP CONVERT(_utf8 '".$url."' USING latin1) COLLATE latin1_swedish_ci";
Related
:)
I'm scripting a little web app with a database connection.
If the URL ends with ?tag=whatever it should only select the records with the categorie (rubrik) whatever in this case. It worked but then I rewrote the code a little and now I can't find the mistake. :D
Here's my php code:
if(isset($_GET['tag'])) {
$rubrik = $_GET['tag'];
$rubrik = mysqli_real_escape_string($conn,$rubrik);
$sql = "SELECT * FROM `projects` WHERE `rubrik`=`". $rubrik ."` ORDER BY `datum` DESC";
$result = $conn->query($sql);
}
else {
$sql = "SELECT * FROM `projects` ORDER BY `datum` DESC";
$result = $conn->query($sql);
}
while($row = $result->fetch_assoc()) {
echo "<a href='project.php?id=".$row["id"]."'><div class='card ". $row["rubrik"] ."'><h4>".$row["name"]."</h4><p>".$row["funktion"]."</p></div></a>";
}
If I just open index.php it works as usual. But if I type index.php?tag=whatever
This error pops up:
Fatal error: Call to a member function fetch_assoc() on a non-object
Does anyone know what I did wrong? Thanks for your help in advance! :)
Your query uses backtick while it should use single quote. Backtick is for database, table or column identifier while single quote is for string or date literal value.
Change from
"SELECT * FROM `projects` WHERE `rubrik`=`". $rubrik ."` ORDER BY `datum` DESC";
to
"SELECT * FROM `projects` WHERE `rubrik`='". $rubrik ."' ORDER BY `datum` DESC";
try updating your code as below
if(isset($_GET['tag'])) {
$rubrik = $_GET['tag'];
$rubrik = mysqli_real_escape_string($conn,$rubrik);
$sql = "SELECT * FROM `projects` WHERE `rubrik`=`". $rubrik ."` ORDER BY `datum` DESC";
$result = $conn->query($sql);
}
else {
$sql = "SELECT * FROM `projects` ORDER BY `datum` DESC";
$result = $conn->query($sql);
}
if( $result){
while($row = $result->fetch_assoc()) {
echo "<a href='project.php?id=".$row["id"]."'><div class='card ". $row["rubrik"] ."'><h4>".$row["name"]."</h4><p>".$row["funktion"]."</p></div></a>";
}
}
Mistake in your select query
"SELECT * FROM `projects` WHERE `rubrik`=`". $rubrik ."` ORDER BY `datum` DESC";// remove ` symbol in php variable $rubrik
I have the following query below which uses the GET value of fname and lname to compare it to a database table:
$query = mysql_query("SELECT * FROM pages
WHERE `fname` = '$fname' OR `lname` = '$lname'
LIMIT $start, $perpage")
or die(mysqli_error());
When I run the code, it only puts into account the 'lname' = '$lname' statement.
What I would like it to do is search for either the fname or lname variables, or both if they both of them are set.
query with LIKE
$query = mysql_query("SELECT * FROM pages
WHERE `fname` LIKE '%$fname%' AND `lname` LIKE '%$lname%'
LIMIT $start, $perpage
");
If i'm doing multiple mysql queries on the same table, occasionally some get skipped.
Why is that?
For example:
<?php
mysql_query("UPDATE `tb` SET `field` = '' WHERE `Id` = '$something'");
mysql_query("UPDATE `tb` SET `field2` = '' WHERE `Id` = '$something'");
mysql_query("UPDATE `tb` SET `field3` = '0' WHERE `Id` = '$something'");
?>
Sometimes one of the queries will not be executed?
Why is that?
-Or is it something wrong with my server not general mysql?
(Obviously I know now to update the same table in the same query, but before that I was very confused as to why it happens, can anyone please explain?)
Thanks!
You do not need to make 3 queries, if you are updating the same rows:
$q = "
UPDATE table
SET field = '',
field2 = '',
field3 = 0
WHERE Id = :id
";
$statement = $pdo->prepare( $q );
$statement->bindParam(':id', $something, PDO::PARAM_INT);
$statement->execute();
Also, you should stop using the ancient mysql_* functions. They are not maintained anymore and process for deprecation has already begun.
Maybe you should avoid the 10+ year old API and learn something for this decade: PDO Tutorial for MySQL Developers.
debug your code to see if a query fails:
$result = mysql_query("UPDATE `tb` SET `field` = '' WHERE `Id` = '$something'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$result = mysql_query("UPDATE `tb` SET `field2` = '' WHERE `Id` = '$something'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$result = mysql_query("UPDATE `tb` SET `field3` = '0' WHERE `Id` = '$something'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
Use the following to debug the code:
mysql_query("UPDATE `tb` SET `field` = '' WHERE `Id` = '$something'") or die(mysql_error());
mysql_query("UPDATE `tb` SET `field2` = '' WHERE `Id` = '$something'") or die(mysql_error());
mysql_query("UPDATE `tb` SET `field3` = '0' WHERE `Id` = '$something'") or die(mysql_error());
UPDATE
Make sure you are escaping $something using:
$something = mysql_real_escape_string($something);
I have this very simple function:
function getCatName($id){
$sql = "SELECT * FROM biznet_category WHERE ID ='".$id."';";
$res = mysql_query ($sql) or die (mysql_error ());
$row = mysql_fetch_assoc ($res);
$name = $row["Name"];
return $name;
}
So with this function I should be able to get the category name, but it doesn't work with the parameter. If I put 8 or 9, the categoryname is displayed correctly.
The id is also passed on like it should, when I print it out, it shows 8 or 9.
I know the solution is quite simple, I just don't see it.
To fix remove the quotes and check the column name for case id or ID. Since the query string is in double quotes you don't have to use the . join
$sql = "SELECT * FROM biznet_category WHERE ID = $id";
You can use curly brackets which I find easier to read
$sql = "SELECT * FROM biznet_category WHERE ID = {$id}";
If you were querying a string rather than an integer you can simply do
$sql = "SELECT * FROM biznet_category WHERE ID = '{$id}'";
$sql = "SELECT * FROM biznet_category WHERE ID ='".$id."';";
To
$sql = "SELECT * FROM biznet_category WHERE ID = ".$id;
Try this
$sql = "SELECT * FROM biznet_category WHERE ID = ".$id;
Is the column name ID spelt correctly?
Due to some help from a recent post, I'm selecting a row by primary key, as follows:
$query ="SELECT * FROM Bowlers WHERE 'key' = '1'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result)or die(mysql_error());
For some reason, the third line of code dies every time, without error. It works fine using other keys, ie WHERE name = 'djs22'.
Any ideas?
You are using single quotes on the field name, you must use backticks.
not ', but `
try
$query ="SELECT * FROM Bowlers WHERE key = '1'";
or
$query ="SELECT * FROM `Bowlers` WHERE `key` = '1'";
instead of
$query ="SELECT * FROM Bowlers WHERE 'key' = '1'";
try using this
$query ="SELECT * FROM Bowlers WHERE `key` = '1'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result)or die(mysql_error());
I just replaced ' ' by .