translator in php and mysql - php

ok i make this one but i have 83000 words in mysql database when i execute this script it will take too much time and some time it not runs. i think this script match every title in mysql database wather it is in the $row['full_story'] or not. so this make the opreation unusable if there is any method i can make this process faster ? or it just match those titles which are used $row['full_story'] code is below
$user_name = "root";
$password = "";
$database = "salar";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "SET NAMES 'utf8'";
mysql_query($SQL);
$SQL = "SELECT * FROM dle_mylinks ORDER BY LENGTH( title ) DESC";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
$row['full_story'] = str_replace ($db_field['title'],"" . $db_field['title'] . "" ,$row['full_story']);
$row['short_story'] = str_replace ($db_field['title'],"" . $db_field['title'] . "" ,$row['short_story']);
}
$mydata =$row['short_story'] . $row['full_story'];
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}

You'll want to iterate through each row in the table in a loop.
Example:
$query = mysql_query("SELECT * FROM `table`");
while($row = mysql_fetch_object($query)){
$mydata = str_replace($row->word,$row->meaning,$mydata);
}

Be careful with str_replace, If you want to replace 'distance' per 'thistance' and after it, replaces 'this' by 'dis' you get 'distance' again (it's a word game, an aproximation to the issue)
Use preg_replace.
Create two arrays (zend framework fetchCol or your favorite lib)
$aWords = $zfDb->fetchCol('select words from table');
$aMeans = $zfDb->fetchCol('select means from table');
And
$mydata = preg_replace($aWords, $aMeans, $mydata);

Related

Code novice, mysql_fetch_array to PDO

I have been looking to convert some older PHP queries to newer PDO method.
This is where I am at:
$servername = "localhost";
$dbName = XXXXXXXXX
$dbUser = XXXXXXXXX;
$dbPass = XXXXXXXXX;
$conn = new PDO("mysql:host=$servername;dbname=XXXXXXXXX", $dbUser,
$dbPass);
$delimiter = "**";
$query = "SELECT * FROM `images` WHERE `portfolio_id` = 'X' AND `image_status` = 'X' ORDER BY rand()";
$conn->query($query);
while($row = mysql_fetch_array($query)){
$sVal = substr($row['image_full'],0);
$nString = $nString.$delimiter.$sVal;
}
Seems like it is going okay till I get to the "mysql_fetch_array" part. Any advice would be greatly appreciated.
If you have a look at these pages it should get you on the right track http://php.net/manual/en/pdostatement.fetch.php and http://php.net/manual/en/pdo.query.php
Updated bit:
foreach ($conn->query($query) as $row) {
$sVal = substr($row['image_full'],0);
$nString = $nString.$delimiter.$sVal;
}
With pdo you can iterate through a successful query like this.
You currently pass a string to mysql_fetch_array.
Try this:
$result = $conn->query($query);
while($row = mysql_fetch_array($result)){
This did the trick:
$result = $conn->query($query);
while($row = mysql_fetch_array($result)){

How to address and/or iterate mysql_fetch_assoc array $row

$aRow = array();
$query = "SELECT id, name FROM myTable";
while ($row = mysqli_fetch_assoc($db, $query)) {
$aRow[] = $row;
}
Now that I have several records in $aRow[], I'd like to iterate the $aRow[id] like this:
foreach ($aRow as $thisRow) {
$anID = $thisRow[id];
$query = "SELECT amount FROM mySubTable WHERE myTableID = {$anID}";
.
.
.
}
That doesn't seem to work - $anID doesn't have the expected value. How do I address the associative PHP array?
ETA: To more finely filter my question, after the WHILE completes, I have an array of DB records in $aRow[] that correspond to the database. Maybe I have: $aRow[1, 'Sally'], $aRow[2, 'Dick'], and $aRow[3, 'Jane'].
How to I assign a new variable to 'Dick'? $myVar = $aRow[?] such that $myVar will equal 'Dick'?
$link = new mysqli('localhost', 'myuser', 'mypass', 'dbname');
$query1 = "SELECT id, name FROM myTable";
$result1 = $link->query($query);
while ($row = result1->fetch_array(MYSQL_ASSOC))
{
$query2 = "SELECT amount FROM mySubTable WHERE myTableID = $row[id]";
$result2 = $link->query($query);
.
.
.
}
May be the above code is what you are looking for

Php retrieving number from database

i'm a newbie to php still.
I'm using phpmyadmin as my database. I have a table called 'lessonno' and a column named 'lesson' in it. I tried using this code to retrieve out the number inside 'lesson'. But it's not printing out anything. Can someone help?
<?php
$server = 'localhost';
$username = '';
$password = '';
$database = 'project';
mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$sql = "SELECT 'lesson' FROM 'lessonno'";
$lesson = $_POST['lesson'];
$result = mysql_query($sql);
?>
<?php
for($i = 1; $i <= $lesson; $i++) {
echo "<div>
<span>Lesson ".$i."</span>
</div>
<br>";
}
?>
You can use something like this:
$sql = "SELECT lesson FROM lessonno";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo $row['lesson'];
}
If you would like to only print out a specific lesson with an certan ID, you can use something along the lines:
$id = $_GET['lessonid']; // If you would have something like index.php?lessonid=36 and you'd like it to only fetch the data for the lesson with the id of 36.
$sql = "SELECT lesson FROM lessonno WHERE id='$id'";
(by looking at the $_POST['lesson'] part, I suppose that's something you might be trying to do as it's in the for loop as well)
Also, I suggest you use mysqli.
And, this:
echo "<div>
<span>Lesson ".$i."</span>
</div>
<br>";
Will just echo the $i as both lesson= and the span with Lesson, which won't grab any information from the actual database but just go with the current number it's at, from the for loop you have.
i have made some changes in your code try this
<?php
$server = 'localhost';
$username = 'root';
$password = '';
$database = 'project';
$conn = mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database, $conn) or die(mysql_error());
$sql = "SELECT `lesson` FROM `lessonno`";
$lesson = $_POST['lesson'];
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$lesson_no = $row['lesson'];
echo "<div>
<span>Lesson ".$lesson_no."</span>
</div>
<br>";
}
?>
Note : mysql_* is deprecated. use mysqli_* OR PDO
For getting Values from DB you need to use something like this
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
For further reference please visit http://in2.php.net/manual/en/function.mysql-fetch-assoc.php
For counting the number of data in your database, just insert this code
$sql = "SELECT 'lesson' FROM 'lessonno'";
$lesson = $_POST['lesson'];
$result = mysql_query($sql);
$count=mysql_num_rows($result);//this will count the number of rows in your table.
echo "<div>
<span>Lesson ".$count."</span>
</div>
<br>";

Joomla 1.7 DB Query does not work when query has an ampersand

I am getting mental on this. I have tried everything so far, for hours. Here is the task:
In a module override, I use this code:
$db =& JFactory::getDBO();
$title = "Analysen & Auswertungen Infos";
$query = "SELECT introtext FROM #__content WHERE title=\"$title\"";
$db->setQuery($query);
$result = $db->loadRow();
echo $result;
This works, but since I am getting the $title dynamically from a variable I need this to work:
$db =& JFactory::getDBO();
$title = "$linktext Infos";
$query = "SELECT introtext FROM #__content WHERE title=\"$title\"";
$db->setQuery($query);
$result = $db->loadRow();
echo $result;
I have 6 variables that populate $linktext in a foreach loop, all work except the one with the string including the "&"...
I tried htmlentities and utf8_encode and different kind (actually all combinations) of " and ' in the query... nothing worked.
Whe I use the following sql query in phpmyadmin it works:
SELECT `introtext` FROM `x999x_content` WHERE `title`="Analysen & Auswertungen Infos"
I am really puzzled over this, and right now very tired and angry...
Any help will be greatly appreciated!!!
Sometimes a good long sleep is the best you can do!
I just used strlen to check the length of $linktext (which I get from DB via a foreach loop) and found that it is longer than the visible chars. This is logical because of the & which is returned as &.
In order to use this $linktext inside a new DB query, all I needed to do was to decode the html entity:
$db =& JFactory::getDBO();
// this is the correct way of doing it
$title = html_entity_decode($linktext)." Infos";
$query = "SELECT introtext FROM #__content WHERE title=\"$title\"";
$db->setQuery($query);
$result = $db->loadRow();
or any other combi for the query (",',`,$query->select ) as pointed out in the other answers
Did you try:
$db =& JFactory::getDBO();
$title = $linktext." Infos";
$query = "SELECT `introtext` FROM `#__content` WHERE `title`='".$title."'";
$db->setQuery($query);
$result = $db->loadRow();
echo $result;
I don't know if it'll work but, I never have any problems and this is how I write my code.
Try changing this line:
$query = "SELECT introtext FROM #__content WHERE title=\"$title\"";
For this:
$query = $db->getQuery( tru );
$query->select( 'introtext' );
$query->from( '#__content' );
$query->where( 'title=' . $db->Quote( $title );
I hope it helped!
I got this working:
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
$keyvalue='101';
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM mytable WHERE keyfield='".$keyvalue."'";
$result = $conn->query($sql);
if($result && $row = $result->fetch_assoc()) {
$fsql='DESCRIBE mytable';
$fresult = $conn->query($fsql);
if($fresult){
while($frow = $fresult->fetch_assoc()) {
$fieldname=$frow['Field'];
echo($fieldname . ' = ' . $row[$fieldname] . '<br><br>');
}
}
}else echo("Failed: " . $sql);
?>

PHP/MySQL Problem

Why does this only print the sites specific content under the first site, and doesn't do it for the other 2?
<?php
echo 'NPSIN Data will be here soon!';
// connect to DB
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to DB');
$dbname = 'npsin';
mysql_select_db($dbname);
// get number of sites
$query = 'select count(*) from sites';
$result = mysql_query($query) or die ('Query failed: ' . mysql_error());
$resultArray = mysql_fetch_array($result);
$numSites = $resultArray[0];
echo "<br><br>";
// get all sites
$query = 'select site_name from sites';
$result = mysql_query($query);
// get site content
$query2 = 'select content_name, site_id from content';
$result2 = mysql_query($query2);
// get site files
// print info
$count = 1;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "Site $count: ";
echo "$row[0]";
echo "<br>";
$contentCount = 1;
while ($row2 = mysql_fetch_array($result2, MYSQL_NUM)) {
$id = $row2[1];
if ($id == ($count - 1)) {
echo "Content $contentCount: ";
echo "$row2[0]";
echo "<br>";
}
$contentCount++;
}
$count++;
}
?>
The problem is that you assume that once your finished looking for the row with the same id as the site row, that it'll reset the $result2 query to the beginning. This means that after you find your first row (unless you were to sort the two queries), that the second pass of the while loop wouldn't have any results left. You should consider caching the inner while loop first and then using an array lookup to get the value.
An even better solution would involve a join from sites to content which wouldn't require this complex matching process. Joins are a VERY important part of SQL, I highly suggest learning how to use them. http://dev.mysql.com/doc/refman/5.0/en/join.html

Categories