Why I can not echo mysql query result when using WHERE statement - php

I have the next code which shows no results:
<?php include ("access.php");
$corpustitle = "Korpus Bilingüe Alemany-Català (de)";
$result = mysqli_query($dbiac, "SELECT corpus FROM corpus_info WHERE title = '$corpustitle'") or die(mysqli_error($dbiac));
while($row = mysqli_fetch_array($result)){
echo $row['corpus']."<br>";
} ?>
But if I take the WHERE statement out of the query I get next results:
$result = mysqli_query($dbiac, "SELECT corpus FROM corpus_info") or die(mysqli_error($dbiac));
banctraddeucat_ca
banctraddeucat_de
banctraddeuspa_de
...etc.
And also if I do the original query at the phpmyadmin I get the result I'm looking for:

Yes, after #delboy1978uk comments I added this line before the query, and that made the difference:
mysqli_set_charset($dbiac, 'utf8');
So the entire code now is:
<?php include ("access.php");
$corpustitle = "Korpus Bilingüe Alemany-Català (de)";
mysqli_set_charset($dbiac, 'utf8');
$result = mysqli_query($dbiac, "SELECT corpus FROM corpus_info WHERE title = '$corpustitle'") or die(mysqli_error($dbiac));
while($row = mysqli_fetch_array($result)){
echo $row['corpus']."<br>";
} ?>
Thanks...

Related

how to remove duplicate results from mysql query in php?

I'm trying to provide a search function for my PHP site. The users should be able to search rows and columns for their desired query, like "search engine". I tried this php code:
<?php
$con = #mysqli_connect("localhost", "root", "", "search");
$output = '';
if(isset($_POST['search'])) {
$search = $_POST['search'];
$search = preg_replace("#[^0-9a-z]i#","", $search);
$query = mysqli_query($con, "SELECT * FROM sites WHERE (site_title LIKE '%$search%' or site_link LIKE '%$search%' or site_keywords LIKE '%$search%')") or die ("Could not search");
$count = mysqli_num_rows($query);
if($count == 0){
$output = "There was no search results!";
print $output;
}else{
while ($row = mysqli_fetch_array($query)) {
$site_keywords = $row ['site_keywords'];
$site_tit = $row ['site_title'];
$site_link = $row ['site_link'];
$output .='<div> '.$site_tit.''.$site_keywords.''.$site_link.'</div>';
print $output;
}
}
}
?>
Everything works just fine but I'm getting duplicate results. I've read a lot of answers and here's I've done so far: I used SELECT DISTINCT * FROM .... and also SELECT DISTINCT site_id FROM .... but didn't return any result. I tries GROUP BY but they didn't remove the duplicates and returned nothing. I also applied PHP array_unique() on $row = mysqli_fetch_array($query) in where condition, but it also didn't return any result.
If I can do this by using only SQL please or I have to remove duplicates by PHP like using a function, please guide me. Thanks in advance.
Move:
print $output;
outside the loop.
Right now $output is being printed every time through the loop. If your results are A,B,C then your output will be A,A,B,A,B,C (with divs, etc.)

How can I get this php to return the entire column of an sql db

I am trying to query a db for an entire column of data, but can't seem to get back more than the first row.
What I have so far is:
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_fetch_array($medicationItemObj, MYSQLI_NUM)){
echo count($row);
}
It's not my intention to just get the number of rows, I just have that there to see how many it was returning and it kept spitting out 1.
When I run the sql at cmd line I get back the full result. 6 items from 6 individual rows. Is mysqli_fetch_array() not designed to do this?
Well, I had a hard time understanding your question but i guess you are looking for this.
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_num_rows($medicationItemObj))
{
echo $row;
}
Or
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
$i = 0;
while ($row = mysqli_fetch_array($medicationItemObj))
{
$medicationItem[] = $row[0];
$i++;
}
echo "Number of Rows: " . $i;
If you just want the number of rows i would suggest using the first method.
http://php.net/manual/en/mysqli-result.num-rows.php
You can wrote your code like below
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
while ($row = mysqli_fetch_assoc($medicationItemObj))
{
echo $row['medication'];
}
I think this you want
You could give this a try:
$results = mysqli_fetch_all($medicationItemObj, MYSQLI_NUM);
First, I would use the object oriented version of this and always use prepared statements!
//prepare SELECT statement
$medicationItemSQL=$connection->prepare("SELECT medication FROM medication");
// execute statement
$medicationItemSQL->execute();
//bind results to a variable
$medicationItemSQL->bind_result($medication);
//fetch data
$medicationItemSQL->fetch();
//close statement
$medicationItemSQL->close();
You can use mysqli_fetch_assoc() as below.
while ($row = mysqli_fetch_assoc($medicationItemObj)) {
echo $row['medication'];
}

[PHP][Mysql] Select and show simple

#GET from Mysql
$select = "SELECT content from content WHERE page = 'index';";
$result = mysql_query($select);
$row = mysql_fetch_row($result);
#Show in diferent places in my page
<?PHP echo $row[0]; ?>
<?PHP echo $row[1]; ?>
I'm using this code above, the problem is:
It's showing only the first result. What did I wrong?
$select = "SELECT content from content WHERE page = 'index';";
$result = mysql_query($select);
//$row = mysql_fetch_row($result);
// mysql_fetch_row() fetches ONLY ONE ROW
// If you want to fetch them all, yes, you must use a loop.
while ($row = mysql_fetch_row($result)) {
$rows[] = $row;
}
// So do this, get all the rows, and then you can use them in different places in your page
// Show in diferent places in your page
<?PHP echo $rows[0][0]; ?>
<?PHP echo $rows[1][0]; ?>
It's better to do this in a for loop - that way it will get all the results each time, rather than hardcoding for a scenario with just two results, etc.
<?php
$select = "SELECT content from content WHERE page = 'index';";
$result = mysql_query($select);
$rows = mysql_fetch_assoc($select);
foreach ($rows as $row) {
echo $row['content']; // associative array with strings for keys
}
?>
Also, you should be using mysqli for security reasons.

Not enter while loop after mysql_fetch_assoc

please take a look at this code :
$sql = "SELECT * FROM shop";
$result = mysql_query($sql);
echo $result;
echo "before lop";
while ($xxx = mysql_fetch_assoc($result)) {
echo "inside lop";
echo $xxx['column_name'];
}
echo "after lop";
When I run such code i receive :
Resource id #244
before lop
after lop
It did not enter while lop, and I really don't know why :(
I used before such code and there were no problems.
Can someone help me?
$sql = "SELECT * FROM shop";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($result);
Check how many records are present in your shop table. I think shop table is empty.That is why not entering in the while loop.
You can do like this
$count = mysql_num_rows($result);
if($count > 0) {
while ($xxx = mysql_fetch_assoc($result)) {
echo $xxx['column_name'];
}
}
I would guess that the call to mysql_fetch_assoc() has returned false, possibly due to no results being returned from the database, this would cause the while loop to not execute even once. I would check the output of var_dump(mysql_fetch_assoc($result)) to ensure that data has been returned.

Displaying one result on a query with LIMIT 10

Today i'm makeing this new question because this is not posible to display one result on a query with limit 10.
Here is the query:
$query = "SELECT * FROM articol WHERE status = 1 ORDER BY data DESC LIMIT 10";
$result = mysql_query($query) or die ("Could not execute query");
while($row = mysql_fetch_array($result))
{
$id = $row["id"];
$titlu = $row["titlu"];
$data = $row["data"];
$desc = $row["continut"];
$part = strip_tags($desc);
}
And this is the echo to display
<link>http://dirlink.ro/articol.php?art_id=<?php echo $id; ?></link>
<title><?php echo $titlu; ?></title>
<description><?php echo substr($part,0,180); ?> ...{Citeste tot} </description>
<pubDate><?php echo $data; ?></pubDate>
The same code is putted on other page, for other category of my website and it's work just fine. I don't understand why at this section is echoing only one result.
All you are doing is reassigning the variables, you are not outputting them, so you will only ever end up outputting the last result.
What you need to do is call them inside the while loop:
$query = "SELECT * FROM articol WHERE status = 1 ORDER BY data DESC LIMIT 10";
$result = mysql_query($query) or die ("Could not execute query");
while($row = mysql_fetch_assoc($result)) {
$id = $row["id"];
$titlu = $row["titlu"];
$data = $row["data"];
$desc = $row["continut"];
$part = strip_tags($desc);
print "<link>http://dirlink.ro/articol.php?art_id=$id</link>\n"
."<title>$titlu</title>\n"
."<description>".substr($part,0,180)." ...{Citeste tot} </description>\n"
."<pubDate>$data</pubDate>\n";
}
Edited a little after I read the question properly... [blush]
I think you want to use mysql_fetch_assoc($result) for named elements, but I could be mistaken.
your echos need to be in the loop if you are trying to list them all; otherwise your values are going to be overwritten and only the last row will be displayed.
I have fixed it: I checked the RSS field with rssvalidator.org and there it showed me the errors and how to fix it.
So I fixed the error, which seem to originate from some &circi;, which is a character of a Romanian language.

Categories