How can I convert this simple query to CakePHP? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
converting simple query to cake query?
Actually I have 1 query but I am unable to convert it into CakePHP query format.
$result = "select
* from esl_userresults, esl_lyrics
where
esl_userresults.esl_songID = esl_lyrics.id
and esl_lyrics.song_name like '%".$esl_keyword."%'" ;
When I convert this query into CakePHP it gives an error like:
esl_userresults.esl_songID unknown column.

You can execute any query using:
<?php
function some_controller_name()
{
$query_to_execute = 'SELECT * .....';
$results = $this->ModelName->query($query_to_execute);
$this->set('results', $results);
}
?>
Hope this helps

Related

PHP not writing SQL result to JSON file [duplicate]

This question already has answers here:
php JSON_encode not working
(4 answers)
Closed 1 year ago.
What I am trying to achieve is to simply put all the rows of a mysqli result to a JSON file.
My code looks like this:
$sth = mysqli_query($mysqli, "SELECT * FROM table");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print_r($rows);
$mysqli->close();
$fileobj = fopen("takeOutItems.json", 'w');
fwrite($fileobj,json_encode($rows));
fclose($fileobj);
printing the $rows arrays shows data correctly.
fwrite, however, does not change anything in takeOutItems.json.
What am I doing wrong?
The issue was that some data elements were not displayed correctly. Adding $mysqli->set_charset("utf8"); resolved the issue.

Using str_replace in table query in MYSQL [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
If you create a variable inside a if statement is it available outside the if statement?
(4 answers)
How to replace "if" statement with a ternary operator ( ? : )?
(16 answers)
If variable equals value php [duplicate]
(4 answers)
Closed 2 years ago.
There are a lot of examples on SO of using str_replace to modify a query that uses variables in MYSQL but I can't find one that solves my problem.
I have the following legacy query that I'm debugging written in PHP and MySQL.
Somewhat simplified it is:
$sql = "SELECT * from MOVIES WHERE cat = '$cat'";
In the event that cat has a certain value, say, "action" I want to check for "adventure";
Let's say you start with query:
$cat = "action";
$sql = "SELECT * FROM MOVIES WHERE cat='$cat'";
I'm trying to modify the query with:
$needle = "cat=".$cat;
$altcat = "adventure";
$altwhere = "cat=".altcat;
$sql = str_replace($needle,$altwhere,$sql); //NOTHING GETS REPLACED...NOT MATCHING Needle
How can I do this? I'm thinking the problem has something to do with use of spaces or apostrophes in the sql string but can't get it to work.
Thanks for any suggestions.
You want to replace "cat='".$cat."'" with "cat='adventure'", not "cat=".$cat with "cat=adventure".
(Though you are inconsistent in saying if there are spaces around the =.)
But you should not do this and should use a placeholder instead.
I would not try to do string substitution on the SQL query. Instead, just use query parameters.
$cat = 'action'; // suppose this is the input to your program
$sql = "SELECT * from MOVIES WHERE cat = ?";
if ($cat == 'action') {
$cat = 'adventure';
}
$stmt = $db->prepare($sql);
$stmt->execute( [ $cat ] );

How do you convert a mysqli_result into a string? [duplicate]

This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 3 years ago.
I'm trying to echo the result of a mysqli_query however I keep getting the error 'Catchable fatal error: Object of class mysqli_result could not be converted to string' on line 'echo $result;'.
Is there any way I can convert it into a string so that it can be echoed? (P.S sorry if this is easy, I'm new to coding.)
My database is successfully connected and the SQL statements definitely work.
$sql= "SELECT ImageURL FROM `unnormalisedtable` WHERE Yeargroup = 9 ORDER BY RAND() LIMIT 1" ;
$result = mysqli_query($db, $sql);
echo $result;
The expected output is that the result of my SQLi query will be printed on screen, however the error is generated instead. Thanks for any help in advance.
Since you limit the selection to one entry use
$row = mysqli_fetch_array($result);
echo $row['ImageURL'];
If you select more than one entry loop over the result.
while($row = mysqli_fetch_array($result)) {
echo $row['ImageURL'];
}

PHP MySQL query not returning records [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 5 years ago.
I know this may sound like duplicate but it's not. I'm trying to do a simple SELECT query on a mysql table through PHP. This is the code I'm using:
<?php
$RTC_DB = mysqli_connect('localhost', 'root', 'telemedia-arte', 'rtc')
or die("Erro na ligação.. ".mysqli_connect_error());
mysqli_set_charset('utf8mb4_unicode_ci');
if (!$RTC_DB) {
echo "error";
} else {
$query = "SELECT * FROM 'prof' WHERE passo = 2";
$videos = mysqli_query($RTC_DB, $query);
if (mysqli_affected_rows($videos) > 0) {
echo "Woo!";
while ($video = mysqli_fetch_assoc($videos)) {
$video_courses[$video["id_aula"]] = $video["tituloaula"];
}
}
mysqli_close($RTC_DB);
}
?>
If the query returns at least one record, it will output "Woo!", but it's not. I tried the query in phpmyadmin and it works.
Can anybody please help me with this? I'm getting very worried with this.
Thanks in advance
Pedro

In zend, how to print a mysql query properly? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to print exact sql query in zend framework ?
In zend profiler, I can only either print the query with question markers (getQuery) or print the parameter array (getQueryParams).
Is there a way to replace all question markers, and print the real sql query?
Thanks!
Something like that should work:
$profile = $this->getQueryProfile($queryId);
$query = $profile->getQuery();
$params = $profile->getQueryParams();
foreach ($params as $par) {
$query = preg_replace('/\\?/', "'" . $par . "'", $query, 1);
}
The framework uses prepared statements so actually this is the real query - in reality its send to the database which parses it and then the params are binded to it and executed.
You can use the __toString() method.
$dbTable = new Application_Model_DbTable_TradeshowBooking();
$select = $dbTable->select();
$select->setIntegrityCheck(false);
$select->where('ends_on + INTERVAL 4 WEEK > ? ', $requestParams['ends_on']);
Zend_Registry::get('logger')->log($select->__toString(), Zend_Log::INFO);

Categories