Select LIKE not working on XAMPP with MariaDB - php

I cannot get "LIKE %M" to work on XAMPP with MariaDB.
$sql = "SELECT * FROM mytable WHERE names LIKE 'm%' ORDER by names ASC";
$result = mysqli_query($connection, $sql) or die("Error in selecting " .
mysqli_error($connection));
$emparray = array();
while($row = mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
this returns a blank screen, no errors or anything.
but if i switch to something like this
$sql = "SELECT * FROM mytable WHERE names = 'SomeName' ORDER by names ASC";
it runs just fine, so I know its not a problem connecting to the DB.
And I have researched through this site for the answer first, so I am pretty confident that my code is correct.
just not sure if this is a problem with XAMPP and/or MariaDB, or if I just missed something stupid.
Thanks in advance, any advice would be appreciated.

figured it out....
one of my names had an umlaut over the O (Ö).
I didn't realize it was there, but once I noticed it and removed it.. it worked just fine.

Related

SELECT data from one table in each database on server

I wanted to get a list of all databases on a specific account on my server using PHP so I have used the following PHP/MySQL taken from this link: PHP - Get list of databases names
$db_conn = mysqli_connect('localhost', 'username', 'password');
//get a list of databases in the account
$result = mysqli_query($db_conn,"SHOW DATABASES");
//display results
while ($row = mysqli_fetch_array($result)) {
echo "database name - ".$row[0]."<br>";
}
This part works fine. But I now want to get the value of one specific row within a table in each of these databases, all databases contain exactly the same tables and therefore the same rows.
So inside the while { } I tried to put a SELECT statement:
$sql = "SELECT option_value FROM sweb_options WHERE option_name = 'siteurl'";
and then echoed $sql but this didn't do anything except output the statement. Any ideas how I can do this please?
Many thanks.
As I said in the comments
Well first of all you need FROM {database}.sweb_options
while ($row = mysqli_fetch_array($result)) {
$sql = "SELECT option_value FROM `{$row[0]}`.`sweb_options` WHERE option_name = 'siteurl'";
}
Otherwise it will use whatever database you connected to with this instance of mysqli
So basically that just tells it which one you want. You can also do Joins and Unions across multiple DB in MySQL this is not true of all Databases though. Something to keep in mind more if you were using PDO instead of mysqli, but still worth mentioning.
Cheers.
Something like this should work:
while ($row = mysqli_fetch_array($result)) {
$sql = "SELECT option_value FROM {$row[0]}.sweb_options WHERE option_name = 'siteurl'";
$result2 = mysqli_query($db_conn, $sql);
// check for success in case table doesn't have option_value column
if ($result2) {
$row2 = mysqli_fetch_assoc($result2);
echo $row2['option_value'];
}
}

Another beginner with a mysqli issue

I'm quite new to working with PHP and databases. So, like all beginners, I'm having problems with the mysqli extension. I was trying to avoid using mysqli_results, as I didn't want to deal with an array and a loop every time I want a simple piece of data. But that might not be possible.
I need to echo $user_count, but nothing seems to be stored there. My code seems to be okay according to the API, but maybe I'm just trying to use the wrong functions altogether. How do I put the result I need into $user_name?
mysqli_query($conn, "SELECT * FROM `wp_users` WHERE 1");
$result_user_count = mysqli_query($conn, "SELECT COUNT(`ID`) FROM `wp_users`");
$user_count = mysqli_fetch_field_direct($result_user_count, 0);
echo $user_count . ' users found on ' . $dbname . ':</br>';
Based on your provided code, you need to change it like below:-
$query = mysqli_query($conn, "SELECT COUNT(ID) AS COUNT FROM wp_users") or die(mysqli_error($conn));
while ($row = mysqli_fetch_assoc($query)) {
echo "Total Users Are:- ". $row["COUNT"];
}
Note:- try to add mysqli error reporting code so that you will what problem actually exist in your query code, and you can easily resolve them. thanks
Try this. comment if not worked.
$query = mysqli_query($conn, "SELECT COUNT(ID) AS COUNT FROM wp_users");
if ($u_count = $mysqli->query($query)) {
while ($res = $u_count->fetch_assoc()) {
echo "Total Users: ". $res["COUNT"];
}
}

Pagination count returning no value

Was hoping someone could give me some help. I have been trying to learn pagination but have run into an issue. when I run this to get my total rows count:
$sql = "SELECT COUNT (*) FROM item WHERE fid='17'";
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
$rows = $row[0];
$rows comes back with no value, I am under the impression that $rows should contain the total number of records from item with the fid of 17, when I run SELECT COUNT (*) FROM item WHERE fid='17' in phpmyadmin it returns 98 which is the correct count. Directly before the above code I use this code to connect to the db, which I use again later to display the records which works fine.
$con=mysqli_connect("$host","$username","$password","$dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
This statement displays records later in the script
$sql = mysqli_query($con,"SELECT * FROM item WHERE fid='17' ORDER BY id DESC $limit ");
So there is data and the info is correct.
I have been following this tutorial on the subject http://www.developphp.com/view.php?tid=1349 and it works like a charm on his example in the video, all I have changed is the database particulars to mine. Can't figure this one out, been stuck for days so now I am bothering you fine folks.
Update: Changed
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
$rows = $row[0];
to
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rows=mysqli_num_rows($result);
// Free result set
mysqli_free_result($result);
}
And everything is working now. Still wish I knew what was wrong with my original code but I will be moving on. Thanks Phil Perry for all your help this morning!
Well, you could SELECT * FROM... and then call mysqli_num_rows($query). Probably slower than SELECT count(*). BTW, I presume that item is not a reserved word in SQL. You can always wrap table and field names in backticks (not quotes ' or "). You could also try SELECT count(*) AS myCount....
try this,
$sql = "SELECT COUNT(*) FROM item WHERE fid='17'";
$query = mysqli_query($sql);
$row = mysqli_fetch_array($query);
$rows = $row[0];

MySQL not getting query, not displaying any errors

Im trying to run a MySQL query, but for some reason its not working.
This is the query im running:
SELECT * FROM applications WHERE status = 0
And my database looks like this:
http://gyazo.com/cc0bfa109e73a771d99a22b5051ee2de
However, num_rows() returns 0 rows...
No errors are displayed...
Try below code hope it will help you because i have tested in localhost.
$sql = "SELECT * FROM applications WHERE status = 0";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo $row['mcUser'];
}
IF its not work's check your table name again.
replace the query with single quotes between 0
$sql = "SELECT * FROM applications WHERE status = '0'";

mySql custom select and display

I am trying to get custom data from my wordpress database, but my knowledge in sql ist that good. Could some one help me?
Please look at my example.
I am trying to echo out image names (filename) from table (wp_ngg_pictures) that have galleryid = 2
So in this example I would get the list of :
bildes-140.jpg
bildes-127.jpg
bildes-133.jpg
bildes-152.jpg
And so on....
Assuming your using PHP all you would do is connect to your database and then do something like this
$result = mysql_query("SELECT * FROM wp_ngg_pictures WHERE galleryid = 2")or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo $row['filename'];
}
If you wanted more specific results it would be a little different.
Fixed the errors.. sorry bout that, still learning myself.
This should Loop though all galleries with id of 2 and echo out the id and file name.
$GetAlbum=2;
$sql = "SELECT * FROM YOURDATABASE.wp_ngg_pictures WHERE galleryid LIKE '%".$GetAlbum."%'";
$result=mysql_query($sql) or die ('Error! Could not get gallery Databse.');
$num=mysql_num_rows($result);
$i=0;
while ($i < $num) {
$gallery=mysql_result($result,$i,"galleryid");
$galleryImg=mysql_result($result,$i,"filename");
echo 'ID '.$gallery.' File '.$galleryImg.'';
}
SELECT filename from wp_ngg_pictures WHERE galleryid = 2

Categories